C++ Constructor Destructor

// constructor_destructor.cpp
// Author: A.G.Raja
// Website: agraja.wordpress.com
// Licence: GPL

#include <iostream>
using namespace std;
class Employee{
char *name;
int age;
double salary;
public: Employee(); //Constructor
void print();
void setval(char *name, int age, double salary);
char* getname() { return name; }
int getage() { return age; }
double getsalary() { return salary; }
~Employee(); //Destructor
};

Employee::Employee()
{
cout<<“Object Created”<<endl;
name=”fresher”;
age=23;
salary=17500;
}
void Employee::print()
{
cout<< name<<“\t”<<age<<“\t”<<salary <<endl;
}
void Employee::setval(char *n, int a, double s)
{
name=n;
age=a;
salary=s;
}

Employee::~Employee()
{
cout<<“Object Destroyed”<<endl;
}

int main()
{
char *NAME;
int AGE;
double SALARY;
//Employee E257 = {“raja”,25,23519.78};
Employee E257;
cout<<“Print Default Values”<<endl;
E257.print(); // Default Values
E257.setval(“raja”,25,23768);
cout<<“Print New Values”<<endl;
E257.print(); // New values
NAME=E257.getname();
AGE=E257.getage();
SALARY=E257.getsalary();
cout<<NAME<<“\t”<<AGE<<“\t”<<SALARY<<endl;
}
// g++ constructor_destructor.cpp

Download here.

constructor.doc

Advertisements