Subscribe this Blog

Wikipedia

Search results

Notifications and New Posts!

Sunday, August 2, 2020

Google Meet Session - 2nd August 2020 C++ TY Mechanical (A and B)

C++ TY Mech AB
Sunday, 2 August10:00 – 12:00
Join with Google Meet
https://meet.google.com/gra-pvjj-iiu
  1. 10 minutes before
Organiser: Sachin Bhosale




Google Meet Session Video Facebook:



With Kind Regards,

To know attendance : Click here

Today's Learning:

Inheritance, types of inheritance and Friend Function


Inheritance is basic feature of C++ i.e. Object Oriented Programming. Where new class is derived 
from existing  base or parent class.
The Process of deriving new class from parent class is called as Inheritance where properties of 
parent class can be used in child or derived class.
New derived class is called as Child or Derived Class where as Class from which we are deriving is 
called as Base or Parent Class.

When 2 or more than two classes are used in the program then classes need to be declared at the start.
Syntax:
class class_name;

Generally in parent class we are using protected access specifiers as we are using 
properties of parent class into child class.
In last child class we can use a private access specifier. Because further we are not inheriting the properties.

Generally when people say parameter/argument. Meaning of parameter and argument is same. 
But the main difference between them is that the parameter is declared in the function, 
where as an argument is passed through at the time of calling the function.

Argument - Actual Argument - Actual Parameter - value or variable passed at the time of calling.
Parameter - Formal Argument - Formal Parameter - scope is local in the function only.

/*
single or simple inheritance
*/
#include<iostream.h>
#include<conio.h>
class A;
class B;
class A
{
protected:
int i;
};
class B:public A
{
int j;
public:
B(int a,int b)
{
i=a;
j=b;
}
void add();
void show();
};

void B:: add()
{
cout<<"\n i+j="<<i+j;
}
void B::show()
{
cout<<"\n i="<<i;
cout<<"\n j="<<j;
}

void main()
{
clrscr();
B b(5,7);
b.show();
b.add();
getch();
}

/*
Multi-level inheritance
*/
#include<iostream.h>
#include<conio.h>
//#include<stdio.h>
class A;
class B;
class C;
class A
{
protected:
int i;
};
class B : public A
{
protected:
int j;
};
class C:public B
{
int k;
public:
C(int a,int b,int c)
{
i=a;
j=b;
k=c;
}
void largest();
void show();
};

void C:: largest()
{
if(i>j&&i>k)
{
cout<<"\n "<<i<<" is largest Number";
}
else if(j>i&&j>k)
{
cout<<"\n "<<j<<" is largest Number";
}
else if(k>i&&k>j)
{
cout<<"\n "<<k<<" is largest Number";
}
else
{
cout<<"\n Three numbers are not different";
}
}
void C::show()
{
cout<<"\n i="<<i;
cout<<"\n j="<<j;
cout<<"\n k="<<k;
}

void main()
{
clrscr();
C c(5,10,77);
c.show();
c.largest();
C d(55,7,10);
d.show();
d.largest();
//printf("\n Size of d object=%d",sizeof(d));
getch();
}

/*
Multiple Inheritance
*/
#include<iostream.h>
#include<conio.h>
//#include<stdio.h>
class A;
class B;
class C;
class A
{
protected:
int i;
public:
/*A(int p)
{
i=p;
}*/
};
class B
{
protected:
int j;
public:
/*void accesscheckfor_A()
{
cout<<"\n value of i="<<i;
} */
};
class C:public A,public B
{
int k;
public:
C(int p,int q,int r)
{
i=p;
j=q;
k=r;
}
void largest();
void show();
};

void C:: largest()
{
if(i>j&&i>k)
{
cout<<"\n "<<i<<" is largest Number";
}
else if(j>i&&j>k)
{
cout<<"\n "<<j<<" is largest Number";
}
else if(k>i&&k>j)
{
cout<<"\n "<<k<<" is largest Number";
}
else
{
cout<<"\n Three numbers are not different";
}
}
void C::show()
{
cout<<"\n i="<<i;
cout<<"\n j="<<j;
cout<<"\n k="<<k;
}

void main()
{
clrscr();
//A a(1);
//B b(2);
C c(5,10,77);
c.show();
c.largest();
C d(55,7,10);
d.show();
d.largest();
//printf("\n Size of d object=%d",sizeof(d));
getch();
}

/*
Hierarchical Inheritance
*/
#include<iostream.h>
#include<conio.h>
class A;
class B;
class C;
class A
{
protected:
int i;
};
class B:public A
{
int j;
public:
B(int p,int q)
{
i=p;
j=q;
}
void accesscheck_B()
{
cout<<"\n value of i="<<i;
cout<<"\n value of j="<<j;
}
};
class C:public A
{
int k;
public:
C(int p,int r)
{
i=p;
k=r;
}
void accesscheck_C()
{
cout<<"\n value of i="<<i;
//j is not accessible to class C
//cout<<"\n value of j="<<j;
cout<<"\n value of k="<<k;
}
};

void main()
{
clrscr();
B b(5,6);
C c(50,60);
b.accesscheck_B();
c.accesscheck_C();
getch();
}

/*
Hybrid Inheritance
*/
#include<iostream.h>
#include<conio.h>
class A;
class B;
class C;
class D;

class A
{
protected:
int i;
};

class B:public A
{
protected:
int j;
public:
void Bfun(int p,int q)
{
i=p;
j=q;
cout<<"\n i="<<i;
cout<<"\n j="<<j;
}
};

class C:public A
{
protected:
int k;
public:
void Cfun(int p,int r)
{
i=p;
k=r;
cout<<"\n i="<<i;
cout<<"\n k="<<k;
}
};

class D:public B,public C
{
private:
int l;
public:
void Dfun(int q,int r,int s)
{
j=q;
k=r;
l=s;
cout<<"\n j="<<j;
cout<<"\n k="<<k;
cout<<"\n l="<<l;
}
};

void main()
{
clrscr();
B b;
C c;
D d;
cout<<"\n Class B";
b.Bfun(5,7);
cout<<"\n Class C";
c.Cfun(50,70);
cout<<"\n Class D";
d.Dfun(500,600,700);
getch();
}

/*
friend function
*/
#include<iostream.h>
#include<conio.h>
class A;
class B;
class A
{
private:
int i;
public:
//declaration of funab as friend function
friend void funab(A,B);
};

class B
{
private:
int j;
public:
//declaration of funab as friend function
friend void funab(A,B);
};

void funab(A a,B b)
{
cout<<"\n Please enter i=";
cin>>a.i;
cout<<"\n Please enter j=";
cin>>b.j;
cout<<"\n i+j="<<a.i+b.j;
}

void main()
{
clrscr();
A a;
B b;
funab(a,b);
getch();
}

No comments:

Post a Comment