Class 3

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

C++

Prepared by Rekha Kak

Friend class
#include

<iostream>
using namespace std;
class CSquare;
class CRectangle {
int width, height;
public:
int area ()
{
return (width * height);
}
void convert( CSquare a);
};
class CSquare {
private:
int side;
public:
void set_side (int a)
{
side=a;
}
friend class CRectangle;
};

Friend class
void CRectangle::convert (CSquare a)
{
width = a.side;
height = a.side;
}
int main () {
CSquare sqr;
CRectangle rect;
sqr.set_side(4);
rect.convert(sqr);
cout << rect.area();
return 0;
}

Inheritance
One of the most important concepts in object-oriented
programming is that of inheritance. Inheritance allows us to
define a class in terms of another class, which makes it easier
to create and maintain an application. This also provides an
opportunity to reuse the code functionality and fast
implementation time.
When creating a class, instead of writing completely new data
members and member functions, the programmer can
designate that the new class should inherit the members of an
existing class. This existing class is called the base class, and
the new class is referred to as the derived class.

Base And Derived Class


A class can be derived from more than one classes, which
means it can inherit data and functions from multiple base
classes. To define a derived class, we use a class derivation
list to specify the base class(es). A class derivation list
names one or more base classes and has the form:
class derived-class: access-specifier base-class
Where access-specifier is one of public, protected, or
private, and base-class is the name of a previously defined
class. If the access-specifier is not used, then it is private by
default.

Derived Classes/ Hierarchical Inheritance


#include <iostream>
using namespace std;
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
class CRectangle: public CPolygon {
public:
int area ()
{ return (width * height); }
};
class CTriangle: public CPolygon {
public:
int area ()
{ return (width * height / 2); }
};

Derived Classes
int main () {
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
cout << rect.area() << endl;
cout << trgl.area() << endl;
return 0;
}

Different access types

Access
protected

public
private

members of the same class


yes

yes

yes

members of derived classes


no

yes

yes

not members
no

yes
no

What is inherited from the base class?

In principle, a derived class inherits every member of a base


class except:
1) its constructor and its destructor
2) its operator=() members
3) its friends
Although the constructors and destructors of the base class
are not inherited themselves, its default constructor (i.e., its
constructor with no parameters) and its destructor are
always called when a new object of a derived class created
or destroyed.
We can change the base constructor to be called from that
of a default constructor, by when a derived object is created
by specifying it in each constructor definition of the derived
class as
derived_class_name(parameters): base_class_name(parameters) { }

Constructor and derived classes

#include <iostream>
using namespace std;
class mother {
public:
mother ()
{
cout << "mother: no parameters\n";
}
mother (int a)
{
cout << "mother: int parameter\n";
}
};
class daughter : public mother {
public:
daughter (int a)
{
cout << "daughter: int parameter\n\n";
}
};

Constructor and derived classes

class son : public mother {


public:
son (int a) : mother (a)
{
cout << "son: int parameter\n\n"; }
};
int main () {
daughter cynthia (0);
son daniel(0);
return 0;
}

Multiple Inheritance

class CRectangle: public CPolygon, public COutput;


class Ctriangle: public CPolygon, public COutput;

Multiple Inheritance

Multiple Inheritance

#include <iostream>
using namespace std;
class CPolygon {
protected:
int width, height;
public:
void set_values (int a, int b)
{ width=a; height=b;}
};
class COutput {
public:
void output (int i);
};
void COutput::output (int i) {
cout << i << endl;
}

Multiple Inheritance

class CRectangle: public CPolygon, public COutput {


public:
int area ()
{ return (width * height); }
};
class CTriangle: public CPolygon, public COutput {
public:
int area ()
{ return (width * height / 2); }
};
int main () {
CRectangle rect;
CTriangle trgl;
rect.set_values (4,5);
trgl.set_values (4,5);
rect.output (rect.area());
trgl.output (trgl.area());
return 0;
}

Ambiguity in Multiple Inheritance

class base1
{
public:
void some_function( )
{ .... ... .... }
};
class base2
{
void some_function( )
{ .... ... .... }
};
class derived : public base1, public base2
{
};
int main()
{
derived obj;
/* Error because compiler can't figure out which function to call either
same_function( ) of base1 or base2 .*/
obj.same_function( )
}

int main()
{
obj.base1::same_function( );
obj.base2::same_function( );
}

/* Function of class base1 is called. */


/* Function of class base2 is called. */

Multi level inheritance

Multi level inheritance


#include<iostream.h>
class top
//base class
{
public :
int a;
void getdata()
{
cout<<"\n\nEnter first Number :::\t";
cin>>a;
}
void putdata()
{
cout<<"\nFirst Number Is :::\t"<<a;
}
};

Multi level inheritance


//First level inheritance
class middle : public top
{
public:
int b;
void square()
{
getdata();
b=a*a;
cout<<"\n\nSquare Is :::"<<b;
}
};
//Second level inheritance
class bottom :public middle
{
public:
int c;
void cube()
{
square();
c=b*a;
cout<<"\n\nCube :::\t"<<c; } }

// class middle is derived_1

// class bottom is derived_2

Multi level inheritance


int main()
{
bottom b1;
b1.cube();
}

Single Inheritance

Hybrid Inheritance

Hybrid inheritance
#include<iostream.h>
class A //Base class
{
public:
int l;
void len()
{
cout<<"\n\nLenght :::\t";
cin>>l;
}
};
class B : public A
{
public:
int b,c;
void l_into_b()
{
len();
cout<<"\n\nBreadth :::\t";
cin>>b;
c=b*l;
}
};

//Inherits property of class A

class C
{
public:
int h;
void height()
{
cout<<"\n\nHeight :::\t";
cin>>h;
}
};
//Hybrid Inheritance Level
class D:public B,public C
{
public:
int res;
void result()
{
l_into_b();
height();
res=h*c;
cout<<"\n\nResult (l*b*h) :::\t"<<res;
}

int main()
{
D d1;
d1.result();
}

Access Specifier

Public Inheritance: When deriving a class from a


public base class, public members of the base class
become public members of the derived class and
protected members of the base class become
protected members of the derived class. A base class's
private members are never accessible directly from a
derived class, but can be accessed through calls to the
public and protected members of the base class.
Protected Inheritance: When deriving from a protected
base class, public and protected members of the base
class become protected members of the derived class.
Private Inheritance: When deriving from a private base
class, public and protected members of the base class
become private members of the derived class.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy