Advertisements
Home > Cpp > C++ Program to implement Constructor Overloading – Q13

C++ Program to implement Constructor Overloading – Q13


Q13. Program to implement Constructor Overloading:

Implement the concept of Constructor Overloading with the help of not less than three different constructors within any class of your choice.

… from College notes (BCA/MCA assignments):

#include <iostream.h>
#include <conio.h>
#include <string.h>

class Cperson{
	private:
		char name[20];
	public:
		Cperson(){
			cout<<"\n Constructor Called."<<endl;
			strcpy(name, "Manoj");
			}
		Cperson(char *str){
			strcpy(name, str);
			}
		Cperson(Cperson &per){
			strcpy(name, per.name);
			}
		void Show_Name(){
			cout<<name;
			}
	};

void main(){
	clrscr();

	Cperson Op1;
	Cperson Op2("Pandey");
	Cperson Op3(Op2);

	cout<<"\n Op1: ";
	Op1.Show_Name();

	cout<<"\n Op2: ";
	Op2.Show_Name();

	cout<<"\n Op3: ";
	Op3.Show_Name();

	getch();
	}

 

Output:

Constructor Called.

Op1: Manoj
Op2: Pandey
Op3: Pandey


Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: