Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,851 questions

51,772 answers

573 users

How to use inheritance with virtual functions in C++

1 Answer

0 votes
#include <iostream>

using std::cout;
using std::endl;

class Base {
public:
	virtual void vFunction()
	{
		cout << "class Base: virtual void vFunction()" << endl;
	}
};

class Derived1 : public Base {
public:
	void vFunction()
	{
		cout << "class Derived1: void vFunction()" << endl;
	}
};

class Derived2 : public Derived1 {
public:
	void vFunction()
	{
		cout << "class Derived2: void vFunction()" << endl;
	}
};

int main()
{
	Base *p;
	Base o;
	Derived1 d1;
	Derived2 d2;

	p = &o;
	p->vFunction();               

	p = &d1;
	p->vFunction();                 

	p = &d2;
	p->vFunction();                 

	return 0;
}



/*
run:

class Base: virtual void vFunction()
class Derived1: void vFunction()
class Derived2: void vFunction()

*/

 



answered Mar 26, 2018 by avibootz

Related questions

2 answers 191 views
1 answer 135 views
1 answer 168 views
1 answer 151 views
1 answer 152 views
1 answer 138 views
...