Sie sind auf Seite 1von 2

1.

Classes in C++:-
 A Class is used to pack data members and member functions together.
 The Class has mechanism to prevent direct access to its members which is central idea of
Object Oriented Programming.
 The Class declaration is also known as formation of new abstract data type. The abstract
data type can be used to as basic data type such as int,float..etc.
 The “Class” is a keyword. The class declaration is same as struct declaration. The
declaration of class is enclosed with curly braces ( { }) and terminated with semicolon (;).
 The data members and member functions can be declared in two sections. That is private
and public.
 The Private and Public keywords are terminated with a colon ( : ).
 The object cannot directly access the data members and member functions declared in
Private section. But it can access the data members and member functions declared in
Public section.
 The Private Members of a class can only be accessed by Public member functions of the
same class. It is also possible to access private member variables directly like public
member variables provided that the class should have at least one public member
variable.
 Both the private and public member variables are stored in consecutive memory locations
in the memory.
 A pointer to member variable provides address of member variable. By applying
increment (++) and decrement (--) operations on pointer, we can access all private and
public member variables of the class.
 The object of a class contains address of the first member variable. It can also be used to
access the private or public data.

Syntax for Class Declaration:- Example:-

Class <class name> Class student


{ {
private: private:
Variable declaration; int sno;
Function prototype declaration; char sname[10];
int marks;
public: void readDetails();
Variable declaration; public:
Function prototype declaration; Void displayDetails();
}; };
2. Declaring Objects:-
 A class declaration only builds the structure of an object.
 The declaration of object is same as declaration of variables of basic data types.
 Defining objects of class data type is known as class instantiation. Only when objects
are created, memory is allocated to them.

Syntax:-
<class name> <object name>

Eg:- int x,y,z; // declaration of integer variables


Student s1,s2,s3; // declaring of objects or class type variables
 An object is an abstract unit with the following properties.
 It is individual.
 It points to thing. Either physical or logical that is identifiable by the user.
 It holds data as well as operation method that handle data.
 Its scope is limited to the block in which it is defined.
Access to class members:- The object can access the public data members and member
functions of a class by using dot (.) and arrow (  ) operators. For simple objects we use dot
operator. Whereas for pointer type objects we will use arrow operator.
Syntax:-
<object name> <operator> <member name>
Example:-
Student s; // s is an object of class Student
s.showDetails();

Das könnte Ihnen auch gefallen