Skip to content

Instantly share code, notes, and snippets.

@pori
Created July 25, 2015 23:33
Show Gist options
  • Save pori/eeb9e51ef70bd46ca725 to your computer and use it in GitHub Desktop.
Save pori/eeb9e51ef70bd46ca725 to your computer and use it in GitHub Desktop.
An example of object oriented concepts done in C++.
#include <iostream>
#include <string>
using std::string;
struct Coordinates {
float latitude;
float longitude;
};
class Person {
public:
Person(string name, string catchPhrase, int age, float cash) {
_name = name;
_catchPhrase = catchPhrase;
_age = age;
_cash = cash;
}
// Age the Person and returnt the new age.
int getOlder() {
return _age++;
}
string getName() { return _name; }
string getCatchPhrase() { return _catchPhrase; }
int getAge() { return _age; }
float getCash() { return _cash; }
private:
string _name;
string _catchPhrase;
int _age;
float _cash;
};
class Employee: public Person {
public:
Employee(string name, string catchPhrase, int age, float cash, string company, Coordinates coordinates): Person(name, catchPhrase, age, cash) {
_company = company;
_coordinates = coordinates;
}
string getCompany() { return _company; }
void setCompany(string company) { _company = company; }
Coordinates getCoordinates() { return _coordinates; }
void setCoordinates(Coordinates coordinates) { _coordinates = coordinates; }
private:
string _company;
Coordinates _coordinates;
};
int main(int argc, char * args[]) {
Coordinates location;
location.latitude = 42.2658365;
location.longitude = -83.7486956;
Person john("John Q. Public", "I just dabble around.", 35, 1000000.0);
std::cout << "Here's a regular guy:\n\n"
<< john.getName() << "\n"
<< john.getCatchPhrase() << "\n"
<< john.getAge() << "\n"
<< john.getCash() << "\n\n";
Employee employee("Michael Jackson", "I'm amazing!", 52, 1000000000.0, "Amazing Pants", location);
std::cout << "Here's another guy:\n\n"
<< employee.getName() << "\n"
<< employee.getAge() << "\n"
<< employee.getCash() << "\n"
<< employee.getCompany() << "\n"
<< "Coordinates: " << employee.getCoordinates().latitude << ", " << employee.getCoordinates().longitude << "\n";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment