Classes consist of both state and behavior Here state means data(variables) and behavior is functions.
class city
{
private:
char name[10];
public:
void putdata()
{
//block of code }};
Classes allowed private, protected, and public data members and member function. By default all data is private.
class city
{
private
char name[10];
protected:
int postalcode;
public:
void putdata()
{
//block of code
}
};
A member function can be defined inside or outside of the class.
If we define member function outside the class then we use a scope resolution operator.
class city
{
private
char name[10];
int postalcode;
public:
void putdata();
};
void city ::putdata()
{
//block of code
}
}
We also create an array of objects in the main function.
class city
{
private
char name[10];
int postalcode;
public:
void putdata();
};
void city ::putdata()
{
//block of code
}
}
Objects can be passed as a parameter to function.
Objects can be returned from the function.
void city (city c1) // pass by value
{
c1.put_data();
}
};
int main()
{
city c2;
c2.put_data(c2);
}
0 Comments