Classes

This is the feature which makes C++ different from other language approach. A class is similar to a structure but the only difference is that class can contain member functions along with variables.

 

 In class there are three types of scope possible .they are private, public and the protected scope.

Private and public are mostly used but protected is mostly used in inheritance and in other cases it has same scope as a private member. 

Public scope means a member declared under public section can be accessed from any part of the program irrespective of the function in which it is called and which all objects use it.

Private scope means a member declared under the private section cannot be accessed from any were in the program but can only be accessed by the member functions of the class itself.

So in a structure all the members have a public scope but in class they can have any of the three scope. By default the members of a class are private.

 

Syntax:

class class name{

public:

Member functions and variables;

private:

Member functions and variables;

protected:

Member functions and variables;

}

Here no semicolon is required after the class declaration .

 

 

 

Member Functions

The functions declared under the class are known as member functions .the idea of using private is that any other function can use or modify the private data of the class. Their are only a few functions which have the permission to access these members.

class student{

int name;

char div;

void read_data();      

}                                //member function

Member functions are functions which belong to the class, and can thus access all of its members -- public, private and protected -- without any restrictions.

 

Constant Member Functions

 

Constant member functions are the functions which can not modify the objects in the class the const keyword is used after the closing parenthesis of the function declaration.

void add( )  const{

Statements ;

}

 

You cannot modify the members of an object in a const member function, but they are the only member function you can call on a constant object.

Make a Free Website with Yola.