Advertisement
Guest User

Untitled

a guest
May 20th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. #include<iostream>
  2. #include<string>
  3. #include<fstream>
  4.  
  5. using namespace std;
  6.  
  7. const int MAX_NAMES = 5;
  8.  
  9. // simple handy function that prints the message and
  10. // waits for the user to press Enter
  11. void wait(string message) {
  12. cout << message << endl;
  13. cin.get();
  14. }
  15.  
  16. // definition of struct Person
  17. // aka class Person
  18. // aka ADT (Abstract Data Type)
  19. // aka User Defined Type
  20. struct Person {
  21. // these are called attributes or member variables
  22. string lastName;
  23. string firstName;
  24. int age;
  25. char gender;
  26.  
  27. };
  28.  
  29.  
  30.  
  31. void print(Person x) {
  32. cout << "Person: " << x.lastName
  33. << " " << x.firstName << " "
  34. << x.age << " "
  35. << x.gender << endl;
  36. }
  37.  
  38.  
  39. Person createDummyPerson() {
  40. Person dummy;
  41.  
  42. dummy.lastName = "dummy";
  43. dummy.firstName = "dummy";
  44. dummy.age = 0;
  45. dummy.gender = ' ';
  46.  
  47. return dummy;
  48. }
  49.  
  50. // creating a ADT called Planet with 3 properties
  51. struct PlanetOldWay {
  52. string name;
  53. double radius;
  54. double mass;
  55. };
  56.  
  57.  
  58.  
  59. struct Planet {
  60. string name;
  61. double radius;
  62. double mass;
  63.  
  64. // define a constructor (ctor)
  65. Planet(string n, double r, double m) {
  66. name = n;
  67. radius = r;
  68. mass = m;
  69. }
  70.  
  71. // here is a member function:
  72. double volume() {
  73. return ((4 / 3)*3.142*radius*radius*radius);
  74. }
  75. };
  76.  
  77.  
  78. struct CountryStruct {
  79. string name;
  80. string capital;
  81. };
  82.  
  83.  
  84. class CountryClass {
  85.  
  86. string name;
  87. string capital;
  88. public:
  89. // setter or mutator methods
  90. void setName(string n) {
  91.  
  92. if (n == "") {
  93. cout << "Error: cannot set name to blank...";
  94. n = "name not set";
  95. }
  96. name = n;
  97. }
  98.  
  99. void setCapital(string c) {
  100. capital = c;
  101. }
  102.  
  103. // getter or accessor methods
  104. string getName() {
  105. return name;
  106. }
  107.  
  108. string getCapital() {
  109. return capital;
  110. }
  111.  
  112. };
  113.  
  114.  
  115.  
  116. class CountryClass2 {
  117. public:
  118. string name;
  119. string capital;
  120. int population;
  121.  
  122. //ctor
  123. CountryClass2(string n, string c, int p) {
  124. name = n;
  125. capital = c;
  126. population = p;
  127. }
  128.  
  129.  
  130. // returns true if population > 1000
  131. bool meetsCriteria() {
  132. if (population > 1000) {
  133. return true;
  134. }
  135. else {
  136. return false;
  137. }
  138. }
  139.  
  140. };
  141.  
  142.  
  143.  
  144.  
  145. int main() {
  146. int i = 10;
  147. double d = 27.5;
  148. char c = 'x';
  149. string s = "hello";
  150.  
  151. // p is a variable of (user defined) type "Person"
  152. // p is an object of "class" Person
  153. Person p;
  154. p.lastName = "Doe";
  155. p.firstName = "John";
  156. p.age = 25;
  157. p.gender = 'M';
  158.  
  159. cout << "1: Values of p:" << p.lastName
  160. << " " << p.firstName << " "
  161. << p.age << " "
  162. << p.gender << endl;
  163.  
  164. p.age = 34;
  165.  
  166. cout << "2: ";
  167. print(p);
  168.  
  169. Person students[100];
  170.  
  171. // set student[0] to be the same as Person p
  172. students[0].firstName = p.firstName;
  173. students[0].lastName = p.lastName;
  174. students[0].age = p.age;
  175. students[0].gender = p.gender;
  176.  
  177.  
  178. cout << "3: ";
  179. print(students[0]);
  180.  
  181.  
  182. // better way to set student[1] to p
  183. students[1] = p;
  184.  
  185. cout << "4: ";
  186. print(students[1]);
  187.  
  188. cout << "5: ";
  189. Person t1 = createDummyPerson();
  190. print(t1);
  191.  
  192.  
  193. PlanetOldWay e;
  194. e.name = "Mars";
  195. e.radius = 3389.5;
  196. e.mass = 6.41e23;
  197.  
  198.  
  199. Planet mars("Mars", 3389.5, 6.41e23);
  200. // calculate volume of mars..
  201.  
  202. // calculating volumes
  203.  
  204. double volume = (4 / 3)*3.142*mars.radius*mars.radius*mars.radius;
  205. cout << "volume=" << volume << endl;
  206.  
  207. // better way:
  208. cout << "2: volume =" << mars.volume() << endl;
  209.  
  210.  
  211. CountryStruct c1;
  212. c1.name = "USA";
  213. c1.capital = "Washington DC";
  214.  
  215.  
  216. CountryStruct c2;
  217. c2.name = "Canada";
  218. c2.capital = "Ottawa";
  219.  
  220. cout << "name:" << c2.name << endl;
  221. cout << "capital:" << c2.capital << endl;
  222.  
  223.  
  224. // using mutators to set variables in class
  225. CountryClass c3;
  226. c3.setName("Mexico");
  227. c3.setCapital("Mexico City");
  228.  
  229. cout << "name:" << c3.getName() << endl;
  230. cout << "capital:" << c3.getCapital() << endl;
  231.  
  232.  
  233. CountryStruct x1;
  234. x1.name = "";
  235. cout << "x1.name=" << x1.name << endl;
  236.  
  237.  
  238. CountryClass x2;
  239. x2.setName("");
  240. cout << "x2.name=" << x2.getName() << endl;
  241.  
  242. CountryClass2 a1("China", "Beijing", 10000);
  243. CountryClass2 a2("Ghana", "GGG", 100);
  244. CountryClass2 a3("Winterfell", "XYZ", 10);
  245.  
  246. cout << "a1.meetsCriteria=" << a1.meetsCriteria() << endl;
  247. cout << "a2.meetsCriteria=" << a2.meetsCriteria() << endl;
  248. cout << "a3.meetsCriteria=" << a3.meetsCriteria() << endl;
  249.  
  250.  
  251.  
  252. wait("Press Enter to continue...");
  253. return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement