Assignment # 2 (Oop Theory)
Assignment # 2 (Oop Theory)
Assignment # 2 (Oop Theory)
Ambiguity in the inheritance create when we have more than one Base class but only one Derived class
and we have same name function in both base class .so, when derived classs inherit the base class so
because of same name function in both base class ,ambiguity is created.
Example::
#include"iostream"
class A{
public:
int x=10;
void display()
cout<<"x="<<x<<endl;
};
class B
public:
int y=20;
void display()
cout<<"y="<<y<<endl;
};
{
public:
int sum;
sum=x+y;
cout<<"sum ="<<sum<<endl;
};
int main()
c obj;
obj.display();
obj.add();
Question # 2:
Define friend functions and friend classes with your own example?
Friend function::
A friend function of a class is defined outside the class scope but it has the right to access all private
and protected members of the class.
Example ::
#include"iostream"
class test{
private:
int meters;
public:
test()
meters=5;
void display()
cout<<"meter ="<<meters;
};
d.meters=d.meters+5;
int main()
test a;
a.display();
addvalue(a);
cout<<endl;
a.display();
}
Friend class:
If a class become a friend class to a other class then it can access the all function i.e private and
protected member of that class.
Example::
#include"iostream"
class A{
int a,b;
public:
void input()
cin>>a>>b;
friend class B;
};
class B{
int c;
public:
c=ob.a+ob.b;
cout<<"sum="<<c;
};
int main()
A t;
B k;
t.input();
k.add(t);
Question # 3:
More than one function with same name, with different working, we call this a polymorphism.
Example ::
#include"iostream"
class A{
public:
};
class B:public A
int s;
public:
B(int x)
s=x;
void display()
cout<<" s="<<s;
}
};
class C:public A
int s2;
public:
C(int x)
s2=x;
void display()
cout<<"s2="<<s2;
};
int main()
A*ptr;
B k(100);
C g(200);
ptr=&k;
ptr->display();
cout<<endl;
ptr=&g;
ptr->display();
}
Question # 4: