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(); }