You are on page 1of 9

14 April 2023 16:19

New Section 9 Page 1


New Section 9 Page 2
New Section 9 Page 3
New Section 9 Page 4
#include <iostream>
using namespace std;
class A
{
protected :
int num;
public :
void show()
{
cout<<"show of A\n";
cout<<"num of A="<<num<<endl;
}
};

class B
{
protected :
int num;
public :
void show()
{
cout<<"show of B\n";
cout<<"num of B="<<num<<endl;
}
};

class C : public A, public B


{
int num;
public:
C()
{
A ::num=20;
B ::num=30;
num=40;
}
void show()
{
cout<<"Show of C"<<endl;
cout<<"num of C="<<num<<endl;
}
};

int main()
{
C o1;
o1.A ::show();
o1.B ::show();
o1.show();
return 0;
}

New Section 9 Page 5


#include<iostream>
using namespace std;
class A{
public:
int a;
A()
{
a=100;
}
};

class B: virtual public A{

};
class C: virtual public A{

};
class D: public B, public C{

};

int main()
{
cout << "a="<<(new D)->a<<endl;
return 0;
}

New Section 9 Page 6


#include <iostream>

using namespace std;

class Base {

public:

void print() {

cout << "Base Function" << endl;

};

class Derived : public Base {

public:

void print() {

cout << "Derived Function" << endl;

};

int main() {

Derived derived1;

derived1.print();

return 0;

#include <iostream>

New Section 9 Page 7


using namespace std;

class Base {

public:

void print() {

cout << "Base Function" << endl;

};

class Derived : public Base {

public:

void print() {

cout << "Derived Function" << endl;

};

int main() {

Derived derived1, derived2;

derived1.print();

// access print() function of the Base class

derived2.Base::print();

return 0;

New Section 9 Page 8


New Section 9 Page 9

You might also like