Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<string>
- #include<fstream>
- using namespace std;
- const int MAX_NAMES = 5;
- // simple handy function that prints the message and
- // waits for the user to press Enter
- void wait(string message) {
- cout << message << endl;
- cin.get();
- }
- // definition of struct Person
- // aka class Person
- // aka ADT (Abstract Data Type)
- // aka User Defined Type
- struct Person {
- // these are called attributes or member variables
- string lastName;
- string firstName;
- int age;
- char gender;
- };
- void print(Person x) {
- cout << "Person: " << x.lastName
- << " " << x.firstName << " "
- << x.age << " "
- << x.gender << endl;
- }
- Person createDummyPerson() {
- Person dummy;
- dummy.lastName = "dummy";
- dummy.firstName = "dummy";
- dummy.age = 0;
- dummy.gender = ' ';
- return dummy;
- }
- // creating a ADT called Planet with 3 properties
- struct PlanetOldWay {
- string name;
- double radius;
- double mass;
- };
- struct Planet {
- string name;
- double radius;
- double mass;
- // define a constructor (ctor)
- Planet(string n, double r, double m) {
- name = n;
- radius = r;
- mass = m;
- }
- // here is a member function:
- double volume() {
- return ((4 / 3)*3.142*radius*radius*radius);
- }
- };
- struct CountryStruct {
- string name;
- string capital;
- };
- class CountryClass {
- string name;
- string capital;
- public:
- // setter or mutator methods
- void setName(string n) {
- if (n == "") {
- cout << "Error: cannot set name to blank...";
- n = "name not set";
- }
- name = n;
- }
- void setCapital(string c) {
- capital = c;
- }
- // getter or accessor methods
- string getName() {
- return name;
- }
- string getCapital() {
- return capital;
- }
- };
- class CountryClass2 {
- public:
- string name;
- string capital;
- int population;
- //ctor
- CountryClass2(string n, string c, int p) {
- name = n;
- capital = c;
- population = p;
- }
- // returns true if population > 1000
- bool meetsCriteria() {
- if (population > 1000) {
- return true;
- }
- else {
- return false;
- }
- }
- };
- int main() {
- int i = 10;
- double d = 27.5;
- char c = 'x';
- string s = "hello";
- // p is a variable of (user defined) type "Person"
- // p is an object of "class" Person
- Person p;
- p.lastName = "Doe";
- p.firstName = "John";
- p.age = 25;
- p.gender = 'M';
- cout << "1: Values of p:" << p.lastName
- << " " << p.firstName << " "
- << p.age << " "
- << p.gender << endl;
- p.age = 34;
- cout << "2: ";
- print(p);
- Person students[100];
- // set student[0] to be the same as Person p
- students[0].firstName = p.firstName;
- students[0].lastName = p.lastName;
- students[0].age = p.age;
- students[0].gender = p.gender;
- cout << "3: ";
- print(students[0]);
- // better way to set student[1] to p
- students[1] = p;
- cout << "4: ";
- print(students[1]);
- cout << "5: ";
- Person t1 = createDummyPerson();
- print(t1);
- PlanetOldWay e;
- e.name = "Mars";
- e.radius = 3389.5;
- e.mass = 6.41e23;
- Planet mars("Mars", 3389.5, 6.41e23);
- // calculate volume of mars..
- // calculating volumes
- double volume = (4 / 3)*3.142*mars.radius*mars.radius*mars.radius;
- cout << "volume=" << volume << endl;
- // better way:
- cout << "2: volume =" << mars.volume() << endl;
- CountryStruct c1;
- c1.name = "USA";
- c1.capital = "Washington DC";
- CountryStruct c2;
- c2.name = "Canada";
- c2.capital = "Ottawa";
- cout << "name:" << c2.name << endl;
- cout << "capital:" << c2.capital << endl;
- // using mutators to set variables in class
- CountryClass c3;
- c3.setName("Mexico");
- c3.setCapital("Mexico City");
- cout << "name:" << c3.getName() << endl;
- cout << "capital:" << c3.getCapital() << endl;
- CountryStruct x1;
- x1.name = "";
- cout << "x1.name=" << x1.name << endl;
- CountryClass x2;
- x2.setName("");
- cout << "x2.name=" << x2.getName() << endl;
- CountryClass2 a1("China", "Beijing", 10000);
- CountryClass2 a2("Ghana", "GGG", 100);
- CountryClass2 a3("Winterfell", "XYZ", 10);
- cout << "a1.meetsCriteria=" << a1.meetsCriteria() << endl;
- cout << "a2.meetsCriteria=" << a2.meetsCriteria() << endl;
- cout << "a3.meetsCriteria=" << a3.meetsCriteria() << endl;
- wait("Press Enter to continue...");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement