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

Object Oriented Programming 2

The document discusses the diamond problem in object oriented programming where two classes inherit from a common base class, and a derived class inherits from both. It explains that this can cause ambiguities when calling methods on the base class. The solution presented is to use virtual inheritance, which ensures only one copy of the base class is inherited by derived classes, avoiding such ambiguities. Examples are provided to demonstrate virtual inheritance and how it resolves the diamond problem.

Uploaded by

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

Object Oriented Programming 2

The document discusses the diamond problem in object oriented programming where two classes inherit from a common base class, and a derived class inherits from both. It explains that this can cause ambiguities when calling methods on the base class. The solution presented is to use virtual inheritance, which ensures only one copy of the base class is inherited by derived classes, avoiding such ambiguities. Examples are provided to demonstrate virtual inheritance and how it resolves the diamond problem.

Uploaded by

abdullah
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

CS-2012

Object Oriented Programming


Lecture no.12
FALL-2023
The Diamond Problem
class A {
public:
void Foo() {}
}
class B : public A {}
class C : public A {}
class D : public B, public C {}
D d;
d.Foo();
is this B's Foo() or C's Foo() ?? ambiguous
What is the solution?
●Call functions explicitly by specifying name of class and
using scope resolution operator to remove ambiguity:

1. Direct solution:
Student::SetAge() or Salaried::SetAge()

2. Virtual inheritance
Solution (virtual inheritance)
class A {
public: void Foo() {}
}

class B : public virtual A {


}

class C : public virtual A {


}

class D : public B, public C {


}

D d;
d.Foo(); // no longer ambiguous
Virtual Inheritance
●Virtual inheritance is a C++ technique that
ensures that only one copy of common
base class's member variables are
inherited by second-level
derivatives (a.k.a. grandchild derived
classes)
Example
class PoweredDevice
{
public:
PoweredDevice(int nPower)
{
cout << "PoweredDevice: " <<
nPower << endl;
}
};
class Scanner: public PoweredDevice {
public:
Scanner(int nScanner, int nPower) :
PoweredDevice(nPower)
{
cout << "Scanner: " <<
nScanner << endl;
}
};

class Printer: public PoweredDevice {


public:
Printer(int nPrinter, int nPower) :
PoweredDevice(nPower)
{
cout << "Printer: " <<
nPrinter << endl;
}
}; Object Oriented Programming-Spring 23 7
Example
class Copier: public Scanner, public Printer
{
public:
Copier(int nScanner, int nPrinter, int nPower) :
Scanner(nScanner, nPower), Printer(nPrinter, nPower)
{
}

};
Example
int main()
{
Copier cCopier(1, 2, 3);
}

What should be the output?


PoweredDevice: 3
Scanner: 1
PoweredDevice: 3
Printer: 2
Class Activity
Write a code for the following
Access functions
• To allow clients to read the value of private data,
the class can provide a get function.
• To allow clients to modify private data, the class
can provide a set function.
#include <iostream.h>

class rectangle { public:


private: rectangle(float, float); // constructor
float height; void draw(); // draw member function
float width; void posn(int, int); // position member function
int xpos; void move(int, int); // move member function
int ypos; float get_height(); // access function
void set_height(float); // access function
};
float rectangle::get_height()
{
return (height);
}
void rectangle::set_height(float h)
{
height = h;
}
void main()
{
rectangle rc(1.0, 3.0);
float value;
value = rc.get_height();
cout << "height: " << value << endl;
rc.set_height(10.0);
value = rc.get_height();
cout << "height: " << value << endl;
}
Friend functions
• Friend functions of an object can "see"
inside the object and access private
member functions and data.
• To declare a function as a friend of a class,
precede the function prototype in the class
definition with the keyword friend.
class rectangle {

friend void set_value(rectangle&, float); // friend declaration

private:
float height;
float width;
int xpos;
int ypos;
public:
rectangle(float, float); // constructor
void draw(); // draw member function
void posn(int, int); // position member function
void move(int, int); // move member function
float get_height(); // access function
void set_height(float); // access function
};
void set_value(rectangle &rc, float h)
{
rc.height = h;
}

void main()
{
rectangle rc(1.0, 3.0);

set_value(rc, 10.0);
}
Example

You might also like