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

C++ Shortnote

The document contains code snippets demonstrating inheritance in C++. The first code shows a base class A and B, derived class C inheriting from both. It demonstrates accessing members of base classes from the derived class. The second code shows a base class A and derived class B and C inheriting virtually from A. It accesses the base class member. The third code overrides a base class method in the derived class. The fourth code accesses the overridden method and calls the base class method explicitly.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

C++ Shortnote

The document contains code snippets demonstrating inheritance in C++. The first code shows a base class A and B, derived class C inheriting from both. It demonstrates accessing members of base classes from the derived class. The second code shows a base class A and derived class B and C inheriting virtually from A. It accesses the base class member. The third code overrides a base class method in the derived class. The fourth code accesses the overridden method and calls the base class method explicitly.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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