You are on page 1of 3

#include<iostream> #include<string> using namespace std; //Inheritance //can be used only if: //derived class IS the same type

that base class //base class IS NOT the same type that derived class class person//base class { public: ~person() { cout<<"destructor called"<<endl; } string get_name() { return name; } void set_name(string n) { name=n; } person(); person(string, int); void input();

void print(float){}; void print() const; protected://accessible by this class //and all derived classes string name; int age; }; person::person() { cout<<"constructor called"<<endl; name=""; age=0; } person::person(string n, int a) { name=n; age=a; } void person::input() { cout<<"Enter name and age"<<endl; cin>>name>>age; } void person::print() const { cout<<"Name "<<name<<" age "<<age<<endl; }

class student: public person //class student is inherited from //class person { public: //all functions from class person //belong to student by default student(); student(string, int, string); private: //all variables from class person //belong to class student string school; }; student::student():person()//call of the //constructor of the base class { school=""; } student::student(string n, int a, string s): person(n,a) { school=s; }

You might also like