Introduction to ArtificiaI Intelligence in Higher Education
C++ prgms 4th unit Inheritance
1. Object Oriented Programming with C++ Inheritance – I
1 | P a g e A n a n d a K u m a r H N
UNIT - 04 / Inheritance – I
Syntax:
class derived_class_name : visibility_mode base_class_name
{
//body of derived class
}
Example:
class BASE
{
private:
protected:
public:
}
class DERIVED : public BASE
{
private:
protected:
public:
}
Single Inheritance: public
class base
{
private:
int a,b;
public:
void initbase(int x,int y)
{
a=x;
b=y;
}
void showbase( )
{
cout<<"a="<<a<<" b="<<b<<endl;
}
};
class derived:public base
{
private:
int m,n;
public:
void initderived(int u,int v)
{
m=u;
n=v;
}
void showderived( )
{
cout<<"m="<<m<<" n="<<n<<endl;
}
};
int main( )
{
base b1; //object of base class
b1.initbase(10,20);
b1.showbase( );
derived d1; //object of derived class
d1.initderived(33,44);
d1.showderived( );
d1.initbase(2,3); //member function of base class is
accessible from object of derived class
d1.showbase( );
}
OUTPUT:
Single Inheritance: private
class base
{
private:
int a,b;
public:
void initbase(int x,int y)
{
a=x;
b=y;
}
void showbase( )
{
cout<<"a="<<a<<" b="<<b<<endl;
}
};
class derived:private base
{
private:
int m,n;
2. Object Oriented Programming with C++ Inheritance – I
2 | P a g e A n a n d a K u m a r H N
public:
void initderived(int u,int v)
{
m=u;
n=v;
}
void showderived( )
{
cout<<"m="<<m<<" n="<<n<<endl;
}
};
int main()
{
base b1;
b1.initbase(10,20);
b1.showbase( );
derived d1;
d1.initderived(33,44);
d1.showderived( );
d1.initbase(2,3); //not accessible since it is
privately inherited
d1.showbase( ); //not accessible
}
ERROR:
class base
{
private:
int a,b;
public:
void initbase(int x,int y)
{
a=x;
b=y;
}
void showbase( )
{
cout<<"a="<<a<<" b="<<b<<endl;
}
};
class derived:private base
{
private:
int m,n;
public:
void initderived(int u,int v)
{
initbase(u,v);
m=u;
n=v;
}
void showderived( )
{
showbase( );
cout<<"m="<<m<<" n="<<n<<endl;
}
};
int main( )
{
base b1;
b1.initbase(10,20);
b1.showbase( );
derived d1;
d1.initderived(33,44);
d1.showderived( );
}
OUTPUT:
Inheritance and protected members:
class x
{
protected:
int i,j;
void display()
{
cout<<i<<j;
}
};
int main()
{
x x1;
cout<<"enter i j";
cin>>x1.i>>x1.j; //ERROR; cannot access
x1.display();
}
ERROR:
3. Object Oriented Programming with C++ Inheritance – I
3 | P a g e A n a n d a K u m a r H N
//accessing protected members of base class
Example 1:
class base
{
protected:
int i,j; //protected data members
public:
void setbase(int x,int y)
{
i=x;
j=y;
}
void showbase( )
{
cout<<"i="<<i<<" j="<<j<<endl;
}
};
class derived: public base
{
int k;
public:
void setk( )
{
k=i*j; //accessing protected members
}
void showderived( )
{
cout<<"k="<<k<<endl;
}
};
int main()
{
derived d1;
d1.setbase(10,12);
d1.showbase( );
d1.setk( );
d1.showderived( );
}
OUTPUT:
Example 2:
class x
{
protected:
int i,j;
void display( )
{
cout<<i<<endl<<j;
}
};
class y: public x
{
public:
void init(int x,int y)
{
i=x;
j=y;
}
void show( )
{
display( );
}
};
int main()
{
y y1;
y1.init(10,90);
y1.show( );
}
OUTPUT:
10
90
Protected base class inheritance:
/*public and protected data becomes
protected members of derived class*/
class base
{
protected:
int i,j;
public:
void setbase(int x,int y)
{
i=x;
j=y;
}
void showbase( )
{
cout<<"i="<<i<<" j="<<j<<endl;
}
};
class derived: protected base
{
int k;
public:
void setk(int x,int y)
{
setbase(x,y);
k=i*j; //accessing protected data
}
4. Object Oriented Programming with C++ Inheritance – I
4 | P a g e A n a n d a K u m a r H N
void showderived()
{
showbase( );
cout<<"k="<<k<<endl;
}
};
int main( )
{
derived d1;
d1.setk(33,3);
d1.showderived( );
d1.setbase( ) // ERROR; protected member
of derived class
d1.showbase( ) // ERROR;
}
OUTPUT:
Visibility Mode:
Access Type
of Member
(in Base
class)
Visibility Mode ( in Derived Class)
Private Protected Public
Private ------------Not Accessible-----------------
Protected Private Protected Protected
Public Private Protected Public
Multiple Inheritances:
class x
{
public:
int i,j;
void displayx()
{
cout<<i<<endl<<j;
}
};
class y
{
public:
int m,n;
void displayy()
{
cout<<endl<<endl;
cout<<m<<endl<<n;
}
};
class z: public x,public y
{
public:
void read()
{
cout<<"enter i j m n";
cin>>i>>j>>m>>n;
}
void displayz()
{
displayx();
displayy();
}
};
int main()
{
z z1;
z1.read();
z1.displayz();
}
OUTPUT:
/* Problem with multiple inheritance
And solving the problem using :: operator*/
class x
{
public:
int i,j;
void display()
{ cout<<"nin class xn";
cout<<i<<endl<<j;
}
};
class y
{
public:
int m,n;
void display()
{
cout<<"nin class yn";
cout<<m<<endl<<n;
}
};
5. Object Oriented Programming with C++ Inheritance – I
5 | P a g e A n a n d a K u m a r H N
class z: public x,public y
{
public:
void read()
{
cout<<"nenter i j m n";
cin>>i>>j>>m>>n;
}
void display()
{
cout<<"nin class zn";
x::display();
y::display();
}
};
int main()
{
z z1;
z1.read();
z1.display();
z1.x::display();
z1.y::display();
}
OUTPUT:
By using :: operator ambiguity in calling functions of
base classes in multiple inheritance can be resolved.