Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
62 views

Assignment # 2 (Oop Theory)

Upcasting and downcasting refer to converting an object from one class type to another during inheritance relationships. Upcasting is the implicit conversion of a derived class object to a base class pointer or reference. This allows a base class reference to point to a derived class object. Downcasting is converting a base class pointer or reference to a derived class pointer or reference. This requires an explicit cast since not all base class objects refer to derived class objects. An example demonstrates upcasting by creating a derived class object, assigning it to a base class reference, and calling a function through the base class. Downcasting is shown by casting the base class reference back to the derived class to access derived members.

Uploaded by

Abdullah Amjad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views

Assignment # 2 (Oop Theory)

Upcasting and downcasting refer to converting an object from one class type to another during inheritance relationships. Upcasting is the implicit conversion of a derived class object to a base class pointer or reference. This allows a base class reference to point to a derived class object. Downcasting is converting a base class pointer or reference to a derived class pointer or reference. This requires an explicit cast since not all base class objects refer to derived class objects. An example demonstrates upcasting by creating a derived class object, assigning it to a base class reference, and calling a function through the base class. Downcasting is shown by casting the base class reference back to the derived class to access derived members.

Uploaded by

Abdullah Amjad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 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?

You might also like