0% found this document useful (0 votes)
35 views5 pages

Practice Examples: Code

The document contains code examples demonstrating polymorphism in C++. The first example shows implementing a pure virtual function in a derived class. The second example shows a base class pointer pointing to a derived class object and calling a virtual function. The third example shows a private virtual function being called through a base class pointer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views5 pages

Practice Examples: Code

The document contains code examples demonstrating polymorphism in C++. The first example shows implementing a pure virtual function in a derived class. The second example shows a base class pointer pointing to a derived class object and calling a virtual function. The third example shows a private virtual function being called through a base class pointer.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

PRACTICE EXAMPLES

NAME: NAGVINDHYA
REG:20BEE1022

Code :
#include <iostream>

using namespace std;


class Base{
public:
virtual void show()=0;};
class Derived :public Base{
public:
void show(){
cout<<"Implementation of virtual funnction in Derived
class\n";}
};

int main(){//Base obj;


Base*b;
Derived d;
b=&d;
b->show();}

code:
#include <iostream>

using namespace std;


class BC{
public:
int b;
void show()
{
cout<<"b="<<b<<"\n";

}
};
class DC: public BC
{
public:
int d;
void show()
{
cout<<"b="<<b<<"\n";
cout<<"d="<<d<<"\n";

}
};
main()
{
BC*bptr;
BC base;
bptr=&base;
bptr->b=100;
cout<<"bptr points to base object \n";
bptr->show();
DC derived;
bptr=&derived;
bptr->b=201;
//Bptr->d=300;%checking this one
cout<<"bptr points to Derived object \n";
bptr->show();
DC*dptr;
dptr=&derived;
dptr->d=400;
cout<<"dptr is Derived type pointer\n";
dptr->show();

code:
#include <iostream>
using namespace std;
class A {
public:
virtual void show()
{
cout << "Base class\n"; }
};
class B: public A
{ private:
virtual void show()
{ cout << "Derived class\n"; }
};
int main() {
A *a;
B b;
a = &b;
a->show(); }

You might also like