Class in C++: Syntax, member functions, data members, member variables

A class is created by the keyword class and it is used to declare objects. An object is an instance of a class.


class class_name {

private data and functions


data and functions


data and functions

} object-list;

access_specifier can be public, private or protected. By default functions and data inside the class are private and can be accessed only by the members of the class. However, by using the access specifier public other parts of the program can access the class members. Protected specifier is used for inheritance.

For Example:

class worker {

char name[100];


void putname(char *n);

void getname(char *n);


double wages;


Functions inside the class are known as member functions and variables inside the class are known as data members or member variables. Some points about data members:

1) A member can’t be an object to a class in which it is declared.

2) A non static member of a class can’t have initialize.

3) A member can be a pointer to the class.

4) A member can’t be declared as auto, extern or register.

Leave a Reply