//A Simple program to show c++ structure can have function
// using c++, GCC complier
#include <iostream>
using namespace std;
struct CRectangle //here we define a user-defined-datatype using 'struct'
{
private:
int x, y;
public:
void set_values (int,int);
int area()
{
return (x*y);
}
};
void CRectangle :: set_values (int a, int b)
{
x = a;
y = b;
}
int main()
{
CRectangle rect; //creating the object (rect) of a class (CRectangle)
rect.set_values (3,4);
cout << "area:" << rect.area();
cin.ignore();
cin.get();
}
______________________________________________________________
also see the same program using class (only difference, we use 'class' instead of 'struct')
// using c++, GCC complier
#include <iostream>
using namespace std;
struct CRectangle //here we define a user-defined-datatype using 'struct'
{
private:
int x, y;
public:
void set_values (int,int);
int area()
{
return (x*y);
}
};
void CRectangle :: set_values (int a, int b)
{
x = a;
y = b;
}
int main()
{
CRectangle rect; //creating the object (rect) of a class (CRectangle)
rect.set_values (3,4);
cout << "area:" << rect.area();
cin.ignore();
cin.get();
}
______________________________________________________________
also see the same program using class (only difference, we use 'class' instead of 'struct')
No comments:
Post a Comment