An overloaded constructor is a constructor which takes arguments. It is also called parameterized constructor.
Example:
#include <iostream>
#include <string>
#include <conio.h>
using namespace std;
class Student
{
public:
Student()
{
m_nAge=0;
strcpy(m_szName,"");
};
Student(char* szName,int nAge)
{
strcpy(m_szName,szName);
m_nAge=nAge;
}
int m_nAge;
char m_szName[20];
};
int main()
{
Student obj;
cout<<"Name: "<<obj.m_szName<<"\r\n"<<"Age: "<< obj.m_nAge<<"\r\n";
Student obj2("John",12);
cout<<"Name: "<<obj2.m_szName<<"\r\n"<<"Age: "<< obj2.m_nAge<<"\r\n";
getch();
return 0;
}
Result:
Name:
Age: 0
Name: John
Age: 12
No comments:
Post a Comment