Advertisement
splash365

inheritance_basic

Jan 20th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Person
  5. {
  6.     string name;
  7.     int age;
  8. public:
  9.     Person(string name, int age)
  10.     {
  11.         this->name = name;
  12.         this->age = age;
  13.         cout << "Constructor: Person Parameter\n";
  14.     }
  15.     void show()
  16.     {
  17.         cout << "Name: " << name << endl;
  18.         cout << "Age: " << age << endl;
  19.     }
  20.     ~Person()
  21.     {
  22.         cout << "Destructing... Person: " << name << endl;
  23.     }
  24. };
  25.  
  26.  
  27. class Result
  28. {
  29.     int theory;
  30.     int seasonal;
  31. public:
  32.     Result() { cout << "Constructor: Result Default\n"; }
  33.     Result(int theory, int seasonal)
  34.     {
  35.         this->theory = theory;
  36.         this->seasonal = seasonal;
  37.         cout << "Constructor: Result Parameter\n";
  38.     }
  39.     void setMarks(int theory, int seasonal)
  40.     {
  41.         this->theory = theory;
  42.         this->seasonal = seasonal;
  43.     }
  44.     void getResult()
  45.     {
  46.         cout << "Theory: " << theory << endl;
  47.         cout << "Seasonal: " << seasonal << endl;
  48.     }
  49.     ~Result()
  50.     {
  51.         cout << "Destructing... Result" << endl;
  52.     }
  53. };
  54.  
  55. class Student : private Result, public Person
  56. {
  57.     int id;
  58. public:
  59.     Student(string name, int age, int id, int theory, int seasonal) : Person(name, age)
  60.     {
  61.         this->id = id;
  62.         setMarks(theory, seasonal); ///This is delegate function
  63.         cout << "Constructor: Student Parameter\n";
  64.     }
  65.     void show()
  66.     {
  67.         Person::show();
  68.         cout << "Id: " << id << endl;
  69.         getResult();
  70.     }
  71.     ~Student()
  72.     {
  73.         cout << "Destructing... student: " << id << endl;
  74.     }
  75. };
  76.  
  77. class weakStudent : public Student
  78. {
  79.     int logs;
  80. public:
  81.     weakStudent(string name, int age, int id, int theory, int seasonal, int logs) : Student(name, age, id, theory, seasonal)
  82.     {
  83.         this->logs = logs;
  84.         cout << "Constructor: weakStudent Parameter\n";
  85.     }
  86.     void show()
  87.     {
  88.         Student::show();
  89.         cout << "Logs: " << logs << endl;
  90.         cout << "This is a weak student\n";
  91.     }
  92.     ~weakStudent()
  93.     {
  94.         cout << "Destructing... weakstudent: " << logs << endl;
  95.     }
  96. };
  97.  
  98. class brilliantStudent : public Student
  99. {
  100.     double cgpa;
  101. public:
  102.     brilliantStudent(string name, int age, int id, int theory, int seasonal, double cgpa) : Student(name, age, id, theory, seasonal)
  103.     {
  104.         this->cgpa = cgpa;
  105.         cout << "Constructor: brilliantStudent Parameter\n";
  106.     }
  107.     void show()
  108.     {
  109.         Student::show();
  110.         cout << "CGPA: " << cgpa << endl;
  111.         cout << "This is a brilliant student\n";
  112.     }
  113.     ~brilliantStudent()
  114.     {
  115.         cout << "Destructing... brilliantstudent: " << cgpa << endl;
  116.     }
  117. };
  118.  
  119. int main()
  120. {
  121.     weakStudent st("Tamim", 20 , 28, 50, 75, 10);
  122.     st.show();
  123.     brilliantStudent st2("RJ", 20 , 39, 300, 299, 3.99);
  124.     st2.show();
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement