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

3 thoughts on “C++ Constructor Destructor

  1. Pingback: C++ struct vs class « Applied Electronics Journal

  2. I know the entry is old, but I wanted to congratulate you on your writing. I liked the blog, I happened to see what new entries in it. Luck and not to give up, because the blog is a luxury. A kiss !!

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