Thursday, March 17, 2011

Cpp program3

/* Q1.Assume a member function , get_num(), with return vlue of type int is declared inside a class called Calcuator.
Write a typical class specifier and declare the member function. Also write the definition of the function outside the class
specifier.
*/
/*
Add a private data member called number of type int to the class Calculator in Q1.
Write a constructor that initialies number to 0.
Assume the constructor is defined within the class spcifier.

*/
# include "iostream"
using namespace std;
# include "stdio.h"

class Calculator
{
protected:
int nData;
private :
int nNumber;
public:
int get_num();

Calculator() // Constructor
{
nNumber=0;
printf("%d\n",nNumber);


}
~Calculator()
{

printf("I am in destructor\n");

}

};


int Calculator ::get_num()
{
/* the member whoes declaration is within the calculator class spcifier consider to be access variabl*/

nNumber = 10000;

printf("%d\n",nNumber);

return nNumber;
}

int main()
{
Calculator obj;
obj.get_num();


return 0;
}


OUTPUT:

0
10000
I am in destructor
Press any key to continue . . .

No comments:

Post a Comment