Pages

Thursday, July 11, 2013

Classes in C++

/*
QUESTION:
1. Write a class "point" that has two private data members x and y. You are required to write multiple

constructors as follows.


Constructor with no arguments:
If this constructor is called initialize x and y to zero.


Constructor with one argument:
If this constructor is called initialize the both x and y to the given value.


constructor with two arguments:
if this constructor is called initialize x and y to the given value accordingly.
 
* Write two member functions setValue(x,y) and printValue() to set and print value of point



accordingly.
 
* Test your class by instantiating it in main().

* Make three objects using above three constructors. Call the printVaule() function to display



the point values for each point object.

2. Write a destructor containing cout statement printing "Destructor is executing ". Notice when

destructor will be called by running the program.

*/
//CODE:

 #include<iostream>
using namespace std;
class point{
 int x;
    int y;
public:

point();
point(int a);
point(int a,int b);
void setValue(int a,int b);
void printValue();
~point();
};
point::point(){
 cout<<"\n\n\nConstructor is being called"<<this;
 x=0;
 y=0;
}

point::point(int a){
x = a;
y = a;
}

point :: point(int a,int b){
x = a;
y = b;
}

void point ::setValue(int a,int b){
x = a;
y = b;
}
void point::printValue(){
 cout<<"\nvalue of x is : "<<this->x;
    cout<<"\nvalue of y is : "<<this->y<<endl;
}
point::~point(){
cout<<"\n\nDestructor is executing "<<endl;
}


//MAIN//
int main(){
point obj1(5),obj2(3,6),obj3;
obj1.printValue();
obj2.printValue();
obj3.printValue();
 
 return 0;
}

No comments:

Post a Comment