Вы находитесь на странице: 1из 7

Question # 1::

Define ambiguity in the inheritance with your own example?

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"

using namespace std;

class A{

public:

int x=10;

void display()

cout<<"x="<<x<<endl;

};

class B

public:

int y=20;

void display()

cout<<"y="<<y<<endl;

};

class c:public A ,public B

{
public:

int sum;

void add (void)

sum=x+y;

cout<<"sum ="<<sum<<endl;

};

int main()

c obj;

obj.display();

obj.add();

So here ambiguity is created.

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"

using namespace std;

class test{

private:

int meters;
public:

test()

meters=5;

void display()

cout<<"meter ="<<meters;

friend void addvalue(test &d);

};

void addvalue(test &d)

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"

using namespace std;

class A{

int a,b;

public:

void input()

cout<<"enter two number";

cin>>a>>b;

friend class B;

};

class B{

int c;

public:

void add(A ob)

c=ob.a+ob.b;

cout<<"sum="<<c;

};

int main()

A t;
B k;

t.input();

k.add(t);

Question # 3:

Why we do polymorphism with example?

More than one function with same name, with different working, we call this a polymorphism.

Example ::

#include"iostream"

using namespace std;

class A{

public:

virtual void display()=0;

};

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:

Explain upcasting and downcasting with your own example?

Вам также может понравиться