Classes in C++
In C++, classes are created using class keyword.
It declares and define a new type which is a combination of code and
data.
The declaration of a class is
similar to that of declaration of a structure.
A class
not inheriting from any other class is declared as:
class class_name {
private data members and
member functions
access specifier:
data and functions
access specifier:
data and functions
.
.
.
} object_list;
The object_list is optional. We
can create objects of class in the main() function also.
Example:
class A
{ int n;
public:
void get();
void show();
};
void A::get() //accepting
value of n
{ cout<<”Enter value of
n:”;
cin>>n;
}
void A::show() // displaying
value of n
{ cout<<”Value of n
is:”<<n;
}
int main()
{ A a; //Object of class A is
created
a.get();
a.show();
}
OUTPUT:
Enter value of n:6
Value of n is:6
No comments:
Post a Comment