Last active
January 26, 2019 18:07
-
-
Save theArjun/2bf3e8212f94289cf2fa1aa59e5ee52a to your computer and use it in GitHub Desktop.
Diamond Inheritance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| #include<string> | |
| using namespace std; | |
| class University{ | |
| private: | |
| string uname; | |
| public: | |
| University():uname("unspecified"){} | |
| University(string u):uname(u){} | |
| virtual void display(){ | |
| cout << "University :" << uname << endl; | |
| } | |
| }; | |
| class AffiliatedCollege:public University{ | |
| private: | |
| string aname; | |
| public: | |
| AffiliatedCollege():University(){ | |
| aname = "Unspecified"; | |
| } | |
| AffiliatedCollege(string u, string a):University(u) | |
| aname= a; | |
| } | |
| void display(){ | |
| University::display(); | |
| cout << "Affiliated College : " << aname << endl; | |
| } | |
| }; | |
| class ConstitutentCollege:public University{ | |
| private: | |
| string cname; | |
| public: | |
| ConstitutentCollege():University(){ | |
| cname = "Unspecified"; | |
| } | |
| ConstitutentCollege(string u, string c):University(u){ | |
| cname= c; | |
| } | |
| void display(){ | |
| University::display(); | |
| cout << "ConstitutentCollege : " << cname << endl; | |
| } | |
| }; | |
| class Student:virtual public ConstitutentCollege,virtual public AffiliatedCollege{ | |
| private: | |
| string name; | |
| string program; | |
| public: | |
| Student():AffiliatedCollege(),ConstitutentCollege(){ | |
| name = "Unnamed"; | |
| program = "Unspecified"; | |
| } | |
| Student(string u,string c, string a, string n, string p):ConstitutentCollege(u,c),AffiliatedCollege(u,a){ | |
| name = n; | |
| program = p; | |
| } | |
| void display(){ | |
| AffiliatedCollege::display(); | |
| ConstitutentCollege::display(); | |
| cout << "Name :" << name << endl; | |
| cout << "Program :" << program << endl; | |
| } | |
| }; | |
| int main(){ | |
| Student s1("PU","CTEVT","GCES","Arjun","BESE"); | |
| s1.display(); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment