Class is a user-defined data type.Class is a collection of similar type of objects having same attribute and common functions.Class is a collection of data members and member functions. It is template and blue print to create an object.
Syntax of class Declaration & define
Class Class_name
{
Access_specifier:
Data members;
---------------;
Access_specifier:
Member function();
Member function();
};
The declaration of a class is similar to structure.The class name specifies the name of the name of the class.The body of class enclosed within braces and terminated by semicolon.The class body contains the declaration of variables and functions.The variables and functions are called class members.They are usually grouped under two sections,namely,private and public to denote which of the members are private and which of them are public.
Creating Objects
The declaration of a class is similar to structure.The class name specifies the name of the name of the class.The body of class enclosed within braces and terminated by semicolon.The class body contains the declaration of variables and functions.The variables and functions are called class members.They are usually grouped under two sections,namely,private and public to denote which of the members are private and which of them are public.
Creating Objects
So firstly discuss what is object.Objects are the basic real entities.They may represent a place,a item and so on that program has to handle.They may also represent user-defined data like vectors,lists.Once a class has been declared, we can create variable of that type by using the class name.For example
Class model
{
data members;
--------------;
member functions;
---------------;
};
model car1;creates a variable car1 of type model.In C++,The class variables are known as objects.Suppose we want to declare a multiple objects of single line using comma operator for example model car1,car2;
Member Access control/Access specifier
There are three type of access specifier.
Public access specifier :- The member functions of class which is specifier as a public can e access outside the class but within a program.To specifier the member as a public are public : keyword.
Ex:- Class model
{
public:
char carname[20];
int cprice;
public:
member fuctions;
}}:
Private access specifier :- The member which is specifier as private can be access within a class and friend class.But is not accessable outside the class.
Ex:- Class model
{
private:
char carname[20];
int cprice;
private:
member fuctions;
}}:
Protected access specifier :- Protected access specifier as private.But provide any one benfit that is member which is specifier as a protected can be access by the child class of a parent class.
Comments
Post a Comment