Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Fiend function add 2

no distance

#include <iostream>

using namespace std;

class Distance

private:

int meter;

public:

60

};

Distance(): meter(0)
{}

//friend function

friendintaddFive(Dist;

// friend function
definition

int addFive(Distanced)

//accessing private
data from non-
member function

d.meter += 5;

return d.meter;
}

int main()

Distance D;

cout<<"Distance: "<<
addFive(D);

return 0;

include <iostream>
using namespace std;
// forward declaration
class B;
class A {
private:
int
numA;
};
public:
A(): numA (12) { }
// friend function declaration
friend int add (A, B);
class B {
private:
int
numB;
public:
B(): numB (1) { }
6
2
};
// friend function declaration
friend int add (A, B);
int add (A objectA, B objectB)
{

}
return (objectA.numA + objectB.numB);
int main()
{
}
A objectA;
B objectB;
cout<<"Sum: "<< add(objectA, objectB);
return 0;

inheritance
A Class can be derived from more than one base class. This is called Multiple
inheritance.
Syntax:
class A{
};
class B{
46
};
4.
class C: public A, public B
{
};
Examples
This example demonstrates the Public and Private Inheritance between the
classes.
#include <iostream>
class base
{
private:
int pridataA;
};
//private data member
protected:
int prodataA;
//protected data member
public:
int pubdataA;
class derivedl: public base
{
public:
void funct()
{
int a;
//public data member
//publically derived class
//error: not accessible
a=pridataA;
a=prodataA;
//ok
//ok
a=pubdataA;
47
};
}
class derived2: public base
{
public:
};
void funct()
{
//privately derived class
int a;
//error: not accessible
a=pridataA;
a=prodataA;
a=pubdataA;
}
int main()
{
int a;
derivedl objdl;
//ok
//ok
a=objdl.pridataA;
a= objdl.prodataA;
//error: not accessible
// error: not accessible
a= objdl.pubdataA;
derived2 objd2;
a=objd2.pridata
A;
a=objd2.prodata
A;
//ok (base public to derivedl)
a=objd2.pubdataA;
48
}
return 0;
//
// error: not accessible
er
ro
r:
// error: not accessible (base private to derived2)
no
t
ac
ce
SS
ib
le

This example will explain the process of multiple inheritance.


Example 1:
#include<iostream>
#include<conio.h>
using namespace std;
class student
{
};
protected:
int rno, m1, m2;
public:
class sports
{
}
void get()
cout<<"Enter the Roll no :";
cin>>rno;
cout<<"Enter the two marks
cin>>ml>>m2;
49
{
protected:
int sm;
public:
{
}
void getsm()
// sm = Sports mark
cout<<"\nEnter the sports mark :";
cin>>sm;
};
class statement:public student, public sports
{
int tot, avg;
public:
void display()
{
tot (m1+m2+sm);
avg=tot/3;
cout<<"\n\n\tRoll No : "<<rno<<"\n\tTotal \t:"<<tot;
cout<<"\n\tAvera ge
}
};
int main()
{
statement obj;
obj.get();
obj.getsm();
obj.display();
: "<<avg;
50
}
getch();
4.3. This example demonstrates the concept of multilevel inheritance between
the
classes.
#include<iostream>
using namespace std;
class basel
{
};
protected:
int number1;
public:
void showA ()
{
}
cout<<"enter a value for first number:"<<endl; cin>>number1;
class base2: public basel
{
protected:
int number2;
public:
void showB ()
{
}
};
cout<<"enter value for second number:"; cin>>number2;
class derive: public base2
51
{
public:
void showC ()
{
showA();
}
};
//accessing basel's
function showB();
//accessing base2's function
cout<<"number1*number2 ="<<number1*number2;
int main()
{
derive obl;
obl.showC () ;
}
cout<<endl;
system ("pause");

You might also like