0% found this document useful (0 votes)
33 views4 pages

Pointers

This document contains code examples demonstrating pointer to derived classes, virtual functions, pure virtual functions and abstract classes, and virtual destructors in C++. It shows a base class and derived class, with a base class pointer assigning to both base and derived class objects. It then demonstrates runtime polymorphism using virtual functions, with a base class pointer calling derived class functions. It provides an example of an abstract base class with pure virtual functions, requiring derived classes to implement the functions. Finally, it shows the importance of virtual destructors by deleting a derived class object using a base class pointer, with and without the destructor defined as virtual.

Uploaded by

whonoiam1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views4 pages

Pointers

This document contains code examples demonstrating pointer to derived classes, virtual functions, pure virtual functions and abstract classes, and virtual destructors in C++. It shows a base class and derived class, with a base class pointer assigning to both base and derived class objects. It then demonstrates runtime polymorphism using virtual functions, with a base class pointer calling derived class functions. It provides an example of an abstract base class with pure virtual functions, requiring derived classes to implement the functions. Finally, it shows the importance of virtual destructors by deleting a derived class object using a base class pointer, with and without the destructor defined as virtual.

Uploaded by

whonoiam1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

//POINTER TO DERIVED CLASS

class base
{
public:
int b;
void show()
{
cout<<"\nb="<<b;
}
};
class derived : public base
{
public:
int d;
void show()
{
cout<<"\n b="<<b<<"\n d="<<d;
}
};
int main(){
base B1;
derived D1;
base *bptr;
bptr=&B1;
cout<<"\nBase class pointer assign address of base class object";
bptr->b=100;
bptr->show();
bptr=&D1;
bptr->b=200;
cout<<"\nBase class pointer assign address of derived class object";
bptr->show();
derived *dptr;
dptr=&D1;
cout<<"\nDerived class pointer assign address of derived class object";
dptr->d=300;
dptr->show();
}

// VIRTUAL FUNCTIONS

class Base {
public:
virtual void show(){
cout << "Base\n"; }
void print(){
cout << "Base print\n";}
};

class Derv1 : public Base {


public:
void show(){
cout << "Derv1\n"; }
void print(){
cout << "derived print\n";}
};
class Derv2 : public Base {
public:
void show(){
cout << "Derv2\n"; }
void print(){
cout << "derived print\n";}
};

int main()
{
Derv1 dv1;
Derv2 dv2;
Base* ptr;
ptr = &dv1; //run time binding
ptr->show();
ptr = &dv2;
ptr->show();
ptr->print() //comile time binding
}

Output:
Derv1
Derv2
Base print

//PURE VIRTUAL FUNCTION AND ABSTRACT CLASS

class Shape{
protected:
float x;
public:
void getData(){cin >> x;}
virtual float calculateArea() = 0;
};
class Square : public Shape
{
public:
float calculateArea()
{ return x*x; }
};
class Circle : public Shape
{
public:
float calculateArea()
{ return 3.14*x*x; }
};

int main()
{
Square s;
Circle c;
cout << "Enter length to calculate the area of a square:";
s.getData();
cout<<"Area of square: " << s.calculateArea();
cout<<"Enter radius to calculate the area of a circle: ";
c.getData();
cout << "Area of circle: " << c.calculateArea();
}

Output:
Enter length to calculate the area of a square: 10
Area of square: 100
Enter radius to calculate the area of a circle: 9
Area of circle: 254.34

// VIRTUAL DESTRUCTOR

#include <iostream>
using namespace std;

class base {
public:
base()
{ cout << "Constructing base\n"; }
~base()
{ cout<< "Destructing base\n"; }
};
class derived: public base {
public:
derived()
{ cout << "Constructing derived\n"; }
~derived()
{ cout << "Destructing derived\n"; }
};
int main()
{
derived *d = new derived();
base *b = d;
delete b;
getchar();
return 0;
}

Output
Constructing base
Constructing derived
Destructing base

#include <iostream>

using namespace std;

class base {
public:
base()
{ cout << "Constructing base\n"; }
virtual ~base()
{ cout << "Destructing base\n"; }
};

class derived : public base {


public:
derived()
{ cout << "Constructing derived\n"; }
~derived()
{ cout << "Destructing derived\n"; }
};

int main()
{
derived *d = new derived();
base *b = d;
delete b;
getchar();
return 0;
}

Output
Constructing base
Constructing derived
Destructing derived
Destructing base

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy