File tree Expand file tree Collapse file tree 7 files changed +127
-0
lines changed
Part 8 - Object Oriented Programming/Class Across Files Expand file tree Collapse file tree 7 files changed +127
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ #include < iostream>
3+ #include " user.h"
4+ #include " teacher.h"
5+ #include " student.h"
6+ using namespace std ;
7+
8+ void show_identity (User &user)
9+ {
10+ user.output ();
11+ }
12+
13+ // Run using g++ main.cpp teacher.cpp student.cpp user.cpp -std=c++11 followed by ./a
14+ int main ()
15+ {
16+ User user;
17+ show_identity (user);
18+
19+ Teacher teacher;// see the hierarchy of inheritance in output
20+ // Polymorphism with base
21+ User &u1 = teacher;
22+ show_identity (u1);
23+
24+ Student student;// see the hierarchy of inheritance in output
25+ User &u2 = student;
26+ show_identity (u2);
27+
28+ /*
29+ Polymorphism - Function Override in sub class
30+ teacher.output();
31+ student.output();
32+ */
33+ }
Original file line number Diff line number Diff line change 1+ #include " student.h"
2+ void Student::output ()
3+ {
4+ cout<<" I am a student\n " ;
5+ }
6+
7+ Student::Student ()
8+ {
9+ cout<<" Student Created\n " ;
10+ }
Original file line number Diff line number Diff line change 1+ #ifndef STUDENT_H
2+ #define STUDENT_H
3+ #include " user.h"
4+ class Student : public User // Inheritance
5+ {
6+ public:
7+ void output ();
8+ Student ();
9+ };
10+ #endif
Original file line number Diff line number Diff line change 1+ #include " teacher.h"
2+ using namespace std ;
3+
4+ void Teacher::output ()
5+ {
6+ cout<<" I am a teacher" <<endl;
7+ }
8+
9+ Teacher::Teacher ()
10+ {
11+ cout<<" Teacher created\n " ;
12+ }
Original file line number Diff line number Diff line change 1+ #ifndef TEACHER_H
2+ #define TEACHER_H
3+
4+ #include < iostream>
5+ #include " user.h"
6+ #include < vector>
7+
8+ class Teacher : public User // Inheritance
9+ {
10+ public:
11+ vector <string> classes_taking;
12+ void output ();
13+ Teacher ();
14+ };
15+ #endif
Original file line number Diff line number Diff line change 1+ #include " user.h"
2+ #include < iostream>
3+ using namespace std ;
4+
5+ // If any class member methods needs definition do it here
6+ // int User::func(param)
7+ // {
8+ // //Body
9+ // }
10+ User::User ()
11+ {
12+ cout<<" User Created\n " ;
13+ }
14+ void User::output ()
15+ {
16+ cout<<" I am an User\n " ;
17+ }
18+
19+ // Friend functions can access private members of class outside the class - using friend functions to access private data members here
20+ ostream & operator << (ostream & output, User user)
21+ {
22+ output <<" First Name : " <<user.first_name <<" \n Last name : " <<user.last_name
23+ <<" \n Status : " <<user.status ;// Accessing private data members
24+ return output;
25+ }
26+
27+ istream & operator >> (istream & input, User &user) // Pass by reference else input here will not be effected in function call area
28+ {
29+ input>>user.first_name >>user.last_name >>
30+ user.status ; // Accessing private data members
31+ return input;
32+ }
Original file line number Diff line number Diff line change 1+ #ifndef USER_H
2+ #define USER_H
3+ #include < iostream>
4+ using namespace std ;
5+ class User
6+ {
7+ string status;
8+ public:
9+ string first_name, last_name;
10+ friend ostream & operator << (ostream & output, User user);
11+ friend istream & operator >> (istream & input, User & user);
12+ User ();
13+ virtual void output (); // Polymorphism - Function to be overwrritten in sub classes
14+ };
15+ #endif
You can’t perform that action at this time.
0 commit comments