Inheritance
"Is a" Relationship

Constructor and Destructor
  1. //Vehicle.h
  2. #ifndef VEHICLE_H
  3. #define VEHICLE_H
  4. #include <iostream>
  5. #include <string>
  6.  
  7. class Vehicle
  8. {
  9. private:
  10. std::string producer;
  11. public:
  12. //constructor
  13. Vehicle(std::string s);
  14.  
  15. std::string getProducer() const {return producer;}
  16. void setProducer(std::string producer);
  17.  
  18. std::string toString();
  19.  
  20. ~Vehicle();
  21. };
  22. #endif
  1. //Vehicle.cpp
  2. #include <iostream>
  3. #include <string>
  4. #include "Vehicle.h"
  5.  
  6. //constructor
  7. Vehicle::Vehicle(std::string s):producer(s)
  8. {
  9. std::cout<<"Vehicle Constructor ..."<<std::endl;
  10. }
  11.  
  12. void Vehicle::setProducer(std::string producer)
  13. {
  14. this->producer = producer;
  15. }
  16.  
  17. std::string Vehicle::toString()
  18. {
  19. std::string temp;
  20. temp = "Vehicle Producer: "+producer;
  21. return temp;
  22. }
  23.  
  24. Vehicle::~Vehicle()
  25. {
  26. std::cout<<"Vehicle Destructor ..."<<std::endl;
  27. }
  1. //Car.h
  2. #ifndef CAR_H
  3. #define CAR_H
  4. #include <iostream>
  5. #include <string>
  6. #include "Vehicle.h"
  7.  
  8. class Car : public Vehicle
  9. {
  10. private:
  11. std::string model;
  12. public:
  13. //constructor
  14. Car(std::string p, std::string m);
  15.  
  16. std::string getModel() const {return model;}
  17. void setModel(std::string model);
  18.  
  19. std::string toString();
  20.  
  21. ~Car();
  22. };
  23. #endif
  1. //Car.cpp
  2. #include <iostream>
  3. #include <string>
  4. #include "Vehicle.h"
  5. #include "Car.h"
  6.  
  7. //constructor
  8. Car::Car(std::string p, std::string m):Vehicle(p), model(m)
  9. {
  10. std::cout<<"Car Constructor ..."<<std::endl;
  11. }
  12.  
  13. void Car::setModel(std::string model)
  14. {
  15. this->model = model;
  16. }
  17.  
  18. std::string Car::toString()
  19. {
  20. std::string temp;
  21. temp = "Car Producer: "+getProducer()+" Model: "+model;
  22. return temp;
  23. }
  24.  
  25. Car::~Car()
  26. {
  27. std::cout<<"Car Destructor ..."<<std::endl;
  28. }
  1. //Bicycle.h
  2. #ifndef BICYCLE_H
  3. #define BICYCLE_H
  4. #include <iostream>
  5. #include <string>
  6. #include "Vehicle.h"
  7.  
  8. class Bicycle : public Vehicle
  9. {
  10. private:
  11. int size;
  12. public:
  13. //constructor
  14. Bicycle(std::string p, int s);
  15.  
  16. int getSize() const {return size;}
  17. void setSize(int s);
  18.  
  19. std::string toString();
  20.  
  21. ~Bicycle();
  22. };
  23. #endif
  1. //Bicycle.cpp
  2. #include <iostream>
  3. #include <string>
  4. #include "Vehicle.h"
  5. #include "Bicycle.h"
  6.  
  7. //constructor
  8. Bicycle::Bicycle(std::string p, int s) : Vehicle(p), size(s)
  9. {
  10. std::cout<<"Bicycle Constructor ..."<<std::endl;
  11. }
  12.  
  13. void Bicycle::setSize(int s)
  14. {
  15. size = s;
  16. }
  17.  
  18. std::string Bicycle::toString()
  19. {
  20. std::string temp;
  21. temp = "Bicycle Producer: "+getProducer()+" Size: "+std::to_string(size);
  22. return temp;
  23. }
  24.  
  25. Bicycle::~Bicycle()
  26. {
  27. std::cout<<"Bicycle Destructor ..."<<std::endl;
  28. }

Class Access Specification

Redefine Functions
  1. //Grand.h
  2. #ifndef GRAND_H
  3. #define GRAND_H
  4. #include <iostream>
  5. class Grand
  6. {
  7. private:
  8. int gx;
  9. protected:
  10. int gy;
  11. public:
  12. int gz;
  13.  
  14. Grand()
  15. {
  16. gx = 1;
  17. gy = 2;
  18. gz = 3;
  19. }
  20.  
  21. int getX() const {return gx;}
  22. int getY() const {return gy;}
  23. int getZ() const {return gz;}
  24.  
  25. void display()
  26. {
  27. std::cout<<gx<<" "<<gy<<" "<<gz<<std::endl;
  28. }
  29. };
  30. #endif
  1. //Parent.h
  2. #ifndef PARENT_H
  3. #define PARENT_H
  4. #include <iostream>
  5. #include "Grand.h"
  6.  
  7. class Parent : public Grand
  8. {
  9. private:
  10. int px;
  11. protected:
  12. int py;
  13. public:
  14. int pz;
  15.  
  16. Parent() : Grand()
  17. {
  18. px = 10;
  19. py = 20;
  20. pz = 30;
  21. }
  22.  
  23. int getX() const {return px;}
  24. int getY() const {return py;}
  25. int getZ() const {return pz;}
  26.  
  27. void display()
  28. {
  29. std::cout<<px<<" "<<py<<" "<<pz<<std::endl;
  30. std::cout<<Grand::getX()<<" "<<gy<<" "<<gz<<std::endl;
  31. }
  32. };
  33. #endif
  1. //Child.h
  2. #ifndef CHILD_H
  3. #define CHILD_H
  4. #include <iostream>
  5. #include "Grand.h"
  6. #include "Parent.h"
  7.  
  8. class Child : public Parent
  9. {
  10. private:
  11. int cx;
  12. protected:
  13. int cy;
  14. public:
  15. int cz;
  16.  
  17. Child() : Parent()
  18. {
  19. cx = 100;
  20. cy = 200;
  21. cz = 300;
  22. }
  23.  
  24. int getX() const {return cx;}
  25. int getY() const {return cy;}
  26. int getZ() const {return cz;}
  27.  
  28. void display()
  29. {
  30. std::cout<<cx<<" "<<cy<<" "<<cz<<std::endl;
  31. std::cout<<Parent::getX()<<" "<<py<<" "<<pz<<std::endl;
  32. std::cout<<Grand::getX()<<" "<<gy<<" "<<gz<<std::endl;
  33. }
  34. };
  35. #endif

Multiple Inheritance

  1. //Faculty.h
  2. #ifndef FACULTY_H
  3. #define FACULTY_H
  4. #include <iostream>
  5. #include <string>
  6.  
  7. class Faculty
  8. {
  9. private:
  10. std::string title;
  11. public:
  12. Faculty(std::string t):title(t)
  13. {
  14. std::cout<<"Faculty Constructor ..."<<std::endl;
  15. }
  16.  
  17. std::string getTitle() const {return title;}
  18.  
  19. std::string toString()
  20. {
  21. std::string temp;
  22. temp = "Faculty Title: "+title;
  23. return temp;
  24. }
  25.  
  26. ~Faculty()
  27. {
  28. std::cout<<"Faculty Destructor ..."<<std::endl;
  29. }
  30. };
  31. #endif
  1. //Student.h
  2. #ifndef STUDENT_H
  3. #define STUDENT_H
  4. #include <iostream>
  5. #include <string>
  6.  
  7. class Student
  8. {
  9. private:
  10. std::string major;
  11. public:
  12. Student(std::string m)
  13. {
  14. major = m;
  15. std::cout<<"Student Constructor ..."<<std::endl;
  16. }
  17.  
  18. std::string getMajor() const {return major;}
  19.  
  20. std::string toString()
  21. {
  22. std::string temp;
  23. temp = "Student Major: "+major;
  24. return temp;
  25. }
  26.  
  27. ~Student()
  28. {
  29. std::cout<<"Student Destructor ..."<<std::endl;
  30. }
  31. };
  32. #endif
  1. //TA.h
  2. #ifndef TA_H
  3. #define TA_H
  4. #include <iostream>
  5. #include "Faculty.h"
  6. #include "Student.h"
  7.  
  8. class TA : public Faculty, Student
  9. {
  10. private:
  11. std::string course;
  12. public:
  13. TA(std::string t, std::string m, std::string c):Faculty(t), Student(m), course(c)
  14. {
  15. std::cout<<"TA Constructor ..."<<std::endl;
  16. }
  17.  
  18. std::string getCourse() const {return course;}
  19.  
  20. std::string toString()
  21. {
  22. std::string temp;
  23. temp = "TA Title: "+getTitle()+" Major: "+getMajor()+" Course: "+course;
  24. return temp;
  25. }
  26.  
  27. ~TA()
  28. {
  29. std::cout<<"TA Destructor ..."<<std::endl;
  30. }
  31. };
  32. #endif
  1. //main.cpp
  2. #include <iostream>
  3. #include <string>
  4. #include "TA.h"
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8. TA ta("GA", "CS", "CSC115");
  9.  
  10. std::cout<<ta.toString()<<std::endl;
  11.  
  12. return 0;
  13. }
Constructor, Copy Constructor, Move Constructor, Copy Assignment, Move Assignment, Destructor
  1. //V.h
  2. #ifndef V_H
  3. #define V_H
  4. #include <iostream>
  5. #include <string>
  6.  
  7. class V
  8. {
  9. private:
  10. int size;
  11. int *array;
  12. public:
  13. //constructor
  14. V(int s):size(s)
  15. {
  16. std::cout<<"Constructor ..."<<std::endl;
  17. array = new int[size];
  18. for(int i = 0; i < size; i++)
  19. array[i] = 10*i;
  20. std::cout<<"----End Constructor ..."<<std::endl;
  21. }
  22.  
  23. //copy constructor
  24. V(const V &right):size(right.size), array(size?new int [size]:nullptr)
  25. {
  26. std::cout<<"Copy Constructor ..."<<std::endl;
  27. std::copy(right.array, right.array+size, array);
  28. std::cout<<"----End Copy Constructor ..."<<std::endl;
  29. }
  30.  
  31. //move constructor
  32. V(V && right):size(0), array(nullptr)
  33. {
  34. std::cout<<"Move Constructor ..."<<std::endl;
  35. size = right.size;
  36. array = right.array;
  37. right.size = 0;
  38. right.array = nullptr;
  39. std::cout<<"----End Move Constructor ..."<<std::endl;
  40. }
  41.  
  42. int *getAddress() const {return array;}
  43. int getSize() const {return size;}
  44.  
  45. void display() const
  46. {
  47. for(int i = 0; i < size; i++)
  48. std::cout<<array[i]<<" ";
  49. std::cout<<std::endl;
  50. }
  51.  
  52. std::string toString() const
  53. {
  54. std::string str = "V: ";
  55. for(int i = 0; i < size; i++)
  56. str += std::to_string(array[i])+" ";
  57. return str;
  58. }
  59.  
  60. V & time(int n)
  61. {
  62. for(int i = 0; i < size; i++)
  63. array[i] *= n;
  64. return *this;
  65. }
  66.  
  67. //copy assignment
  68. const V& operator=(const V &right)
  69. {
  70. std::cout<<"Copy Assignment ..."<<size<<std::endl;
  71. if(this != &right)
  72. {
  73. V temp(right);
  74. //std::swap(size, temp.size);
  75. //std::swap(array, temp.array);
  76. std::swap(*this, temp);
  77. }
  78. std::cout<<"----End Assignment ..."<<std::endl;
  79.  
  80. return *this;
  81. }
  82.  
  83. //move assignment
  84. V & operator=(V&& right)
  85. {
  86. std::cout<<"Move Assignment ..."<<std::endl;
  87. if(this != &right)
  88. {
  89. delete [] array;
  90. array = 0;
  91. size = 0;
  92.  
  93. size = right.size;
  94. array = right.array;
  95.  
  96. right.size = 0;
  97. right.array = nullptr;
  98. }
  99. std::cout<<"----End Move Assignment ..."<<std::endl;
  100.  
  101. return *this;
  102. }
  103.  
  104. ~V()
  105. {
  106. std::cout<<"Destructor ..."<<size<<std::endl;
  107. delete array;
  108. array = 0;
  109. size = 0;
  110. }
  111. };
  112. #endif
  113.  
  114. //VV.h
  115. #ifndef VV_H
  116. #define VV_H
  117. #include <iostream>
  118. #include <string>
  119. #include "V.h"
  120.  
  121. class VV : public V
  122. {
  123. private:
  124. int type;
  125. public:
  126. //constructor inheritance
  127. VV(int t, int s):type(t), V(s)
  128. {
  129. std::cout<<"WW Constructor ..."<<std::endl;
  130. std::cout<<"----End WW Constructor ..."<<std::endl;
  131. }
  132.  
  133. //copy constructor inheritance
  134. VV(const VV &right):type(right.type), V(right)
  135. {
  136. std::cout<<"VV Copy Constructor ..."<<std::endl;
  137. std::cout<<"----End VV Copy Constructor ..."<<std::endl;
  138. }
  139.  
  140. //move constructor
  141. VV(VV && right):type(right.type), V(std::move(right))
  142. {
  143. std::cout<<"VV Move Constructor ..."<<std::endl;
  144. std::cout<<"----End VV Move Constructor ..."<<std::endl;
  145. }
  146.  
  147. void display() const
  148. {
  149. std::cout<<"Type: "<<type<<" -- ";
  150. V::display();
  151. }
  152.  
  153. //copy assignment
  154. const VV& operator=(const VV &right)
  155. {
  156. std::cout<<"VV Copy Assignment ..."<<std::endl;
  157. type = right.type;
  158. V::operator=(right);
  159. std::cout<<"----End VV Assignment ..."<<std::endl;
  160.  
  161. return *this;
  162. }
  163.  
  164. //move assignment
  165. VV & operator=(VV&& right)
  166. {
  167. std::cout<<"VV Move Assignment ..."<<std::endl;
  168. type = right.type;
  169. V::operator=(std::move(right));
  170. std::cout<<"----End VV Move Assignment ..."<<std::endl;
  171.  
  172. return *this;
  173. }
  174.  
  175. ~VV()
  176. {
  177. std::cout<<"VV Destructor ..."<<std::endl;
  178. }
  179. };
  180. #endif
  181.  
  182. //main.cpp
  183. #include <iostream>
  184. #include "VV.h"
  185.  
  186. int main(int argc, char *argv[])
  187. {
  188. //V constructor
  189. //VV constructor
  190. VV vv(1, 2);
  191. vv.display();//0 10
  192.  
  193. //V copy constructor
  194. //VV copy constructor
  195. VV vv2(vv);
  196. vv.display();//0 10
  197. vv2.display();//0 10
  198.  
  199. //V move constructor
  200. //VV move constructor
  201. VV vv3(std::move(vv));
  202. vv.display();//empty
  203. vv3.display();//0 10
  204.  
  205. //VV copy assignment
  206. //V copy assignment
  207. //V copy constructor, temp(right)
  208. //V move assignment, swap
  209. //V move assignment
  210. //V move assignment
  211. VV vv4(2, 4);
  212. vv4.display();//0 10 20 30
  213. vv4 = vv3;
  214. vv3.display();//0 10
  215. vv4.display();//0 10
  216.  
  217. //VV move assignment
  218. //V move assignment
  219. VV vv5(3, 8);
  220. vv5.display();//0 10 20 30 40 50 60 70
  221. vv4 = std::move(vv5);
  222. vv5.display();//empty
  223. vv4.display();//0 10 20 30 40 50 60 70
  224.  
  225. return 0;
  226. }