Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
GreenGeeks Web Hosting - Faster, Scalable & Eco-Friendly

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,917 questions

11,028 answers

573 users

How to use base class and derived class with pointers in C++

1 Answer

0 votes
#include <iostream>

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

class Base {
	char name[30];
public:
	void set_name(char *s) {
		strcpy(name, s);
	}
	void print_name() {
		cout << name << endl;
	}
};

class DerivedClass : public Base {
	char language[20];
public:
	void set_language(char *s) {
		strcpy(language, s);
	}
	void print_language() {
		cout << language << endl;
	}
};

int main()
{
	Base *p;
	Base bobj;

	p = &bobj;
	p->set_name("Cora");
	bobj.print_name();
	
	DerivedClass *dp;
	DerivedClass dobj;

	p = &dobj;
	p->set_name("Emmett");
	p->print_name();
	
	dobj.print_name();

	dp = &dobj;
	dp->set_language("C++");
	dp->print_language();

	return 0;
}

/*
run:

Cora
Emmett
Emmett
C++

*/

 




answered Mar 17 by avibootz
...