Classes and Objects in C++

C++ is a multi-paradigm programming language. itmaintenances different programming styles.One of the widespread ways to solve a programming problem is by making objects, recognized as object-oriented style of programming.

C++ maintenances object-oriented style of programming which lets you to distribute complex problems into smaller groups by generating objects.

Object is merely a collection of data and functions that act on those data.

C++ Class

  • A class is clear in C++ using keywordclass tracked by the name of class.The body of class is defined inside the curly brackets alsoended by a semicolon at the end.
  • The Class name must start with an uppercase letter. If class name is prepared of more than one word, then first letter of each word must be in uppercase. g.

class Study, class StudyTonight

  • Classes hold, data members and member functions, and the access of these data members and variable rest onon the access specifiers .
  • Class’s member functions can be well-defined inside the class definition or else outside the class definition.
  • Class in C++ are alike to structures in C, the only dissimilarity being, class defaults to private access control, where as structure defaults to public.
  • All the features of OOPS, turn around classes in C++. Inheritance, Encapsulation, Abstraction etc.
  • Objects of class embraces separate copies of data members. We can create as many objects of a class as we want.
  • Classes do possess more characteristics, like we can make abstract classes, immutable classes.


   // some data
   // some functions

Example: Class in C++

Here, we well-defined a class named Test.having two data members: data1 and data2 and two member functions: function1() and function2().

You may have observed two keywords: private and public in the above example.

  • The private keyword creates data and functions private. Private data and functions can be retrieved only from inside the same class.
  • The public keyword marks data and functions public. Public data and functions can be accessed out of the class.
  • In this case,,data1 and data2 are private members where as function1() and function2() are public members.
  • If you attempt to access private data from outside of the class, compiler throws error. This feature inOOP is recognized as data hiding.

  Defining objects

When class is well-defined, only the description for the object is defined; no memory or else storage is allocated.

To use the data and access functions defined in the class, you must to generate objects.



You can create objects of Test class :

Here, two objects o1 and o2 of Test class are produced.

In the above class Test, data1 and data2 are data members and function1() and function2() are member functions.


Data Member

  • Itmay be of any type, containing classes previously defined, pointers to objects of any type, or even references to objects of any type.
  • Data memberscould be private or public, but are classicallysaid private so that values may only be changed at the inclination of the class function members.

 Member Function?

  • Data members contain members that are declared with any of the fundamental types, as well as other types, comprising pointer, reference, array types, bit fields, also user-defined types.
  • You can declare a data member the similar way as a variable, except that explicit initializers are not permissible inside the class definition. Though, a const static data member of integral or else enumeration type may have an explicit initializer.
  • If an array is acknowledged as a nonstatic class member, you must state all of the dimensions of the array.
  • A class can have members that are of a class type or else are pointers or references to a class type. Members that are of a class type must be of a class type that has been earlier declared.
  • Anpartial class type can be used in a member affirmation as long as the size of the class is not desirable.


Accessing data members

Member access defines if a class member is manageable in an expression or declaration. Assume x is a member of class A. Class member x can be stated to have one of the following levels of availability:

  • public: x can be used anywhere without the access limitations defined by private or else protected.
  • private: x can be used merely by the members and friends of class A.
  • protected: x can be used only by the members and friends of class A. the members and friends of classes derived from class A.

Members of classes specified with the keyword class are private by default. Members of classes professed with the keyword struct otherwise union are public by default.

To switch the access of a class member, you usage one of the access specifiers public, private, or else protected as a label in a class member list.


The following table shows the access of data members inseveral scopes of the above example.

Scope A::a A::b A::c
function B::f() No access. Member A::a is private. Access. Member A::b is public. Access. Class B inherits from A.
function C::f() Access. Class C is a friend of A. Access. Member A::b is public. Access. Class C is a friend of A.
object y in


No access. Member y.a is private. Access. Member y.a is public. No access. Member y.c is protected.
object z in main() No access. Member z.a is private. Access. Member z.a is public. No access. Member z.c is protected.
  • An access specifier specifies the approachability of members that follow it till the next access specifieror else until the end of the class definition.
  • You can use any number of access specifiers in any order. If you later define a class member in its class definition, its access specification must be the alike as its declaration.


class A {
class B;
class B { };
  • The compiler will not permit the definition of class B as this class has by now been declared as private.
  • A class member has the same access control irrespective whether it has been defined within its class or outside its class.
  • Access control relates to names. if you add access control to a typedef name, it marks only the typedef name.


Here,The compiler will let the declaration A::C x as the typedef name A::C is public. The compiler would not permit the declaration A::B y since A::B is private. 

 Class access modifiers 

C++ uses the three modifiers namedpublic , protected , and private . C# has themodifiers public , protected and internal , private , plus protected internal .

before studying how to describe class and its objects, lets first rapidly learn what are access specifiers.

Access specifiers in C++ class defines the access control rules. C++ has 3 new keywords presented, viz.,

  1. public
  2. private
  3. protected

These access specifiers are used to set limitations for accessibility of members of class be it data members or else member functions

Access specifiers in the program, are monitored by a colon. You can use either one, two or else all 3 specifiers in the same class to establish different boundaries for different class members. They alter the boundary for all the statements that follow them.


It  means all the class members stated under public will be accessible to everyone. The data members and member functions professed public can be accessed by other classes too. Henceforth there are chances that they might alter them.

public:   // public access specifier
int x;            // Data Member Declaration 
void display();   // Member Function decaration


It means that no one can access the class members stated private outside that class. If someone attempts to access the private member, they will catch a compile time error. By default class variables also member functions are private.

private:   // private access specifier
int x;            // Data Member Declaration 
void display();   // Member Function decaration


it is the last access specifier, and it is alike to private, it creates class member unreachable outside the class. Theycan be retrievedby any subclass of that class. If class A is genetic by class B, then class B is subclass of class A.

protected:   // protected access specifier
int x;            // Data Member Declaration 
void display();   // Member Function decaration

Leave A Comment

Your email address will not be published. Required fields are marked *