Archive
Posts Tagged ‘Constructor Overloading’
C++ Program to implement Constructor Overloading – Q13
January 13, 2010
Leave a comment
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
Categories: Cpp
Constructor Overloading, Cpp, Cpp Constructors
Java Program to show Overloading Constructors – Q21
January 11, 2009
Leave a comment
Q21: Java Program to show overloading constructors
Constructors are used to create instances of an object and can be overloaded by creating more than one Constructor in a class with same name but having different signatures.
class Emp_class{
private String name;
private int salary;
public Emp_class(){
name = "Manoj Pandey";
salary = 10500;
}
public Emp_class(String n, int s){
name = n;
salary = s;
}
public Emp_class(Emp_class Oemp){
name = Oemp.name;
salary = Oemp.salary;
}
public void show(){
System.out.println("\n Name: " + name);
System.out.println(" Salary: " + salary);
}
}
class ctor_ovld{
public static void main(String args[]){
Emp_class Oe1 = new Emp_class();
Oe1.show();
Emp_class Oe2 = new Emp_class("Nitin", 25050);
Oe2.show();
Emp_class Oe3 = new Emp_class("Bhuwanesh", 30050);
Oe3.show();
Emp_class Oe4 = Oe2;
Oe4.show();
}
}
… from College notes (BCA/MCA assignments):
Categories: Java
Constructor Overloading, Java OO Programs




