Class and Object in C++
C++ is a multi-paradigm programming language. Means, it supports different programming styles.
One of the popular ways to solve a programming problem is by creating objects, known as object-oriented style of programming.
C++ supports object-oriented (OO) style of programming.
As the name object-oriented programming suggests, this approach deals with objects.
In object-oriented programming language, the data and functions are bundled together as as self-contained unit called an object. A class is an extended concept similar to the structure in C programming language. In C++ programming language, class describes both the properties (data) and behaviors (functions) of objects.
What is Class in C++ ?
A Class acts as a description or blueprint for objects.
The properties of class are called attributes or fields.
The behaviors of an object of a class are known as operations, and are defined using functions.
Fields and methods(functions) are collectively called member of class.
We declare a class by using class keyword.
Syntax:
class class_name { access_mode: data_type member_var1; data_type member_var2; functions };
Where class_name is a valid identifier for the class.The class body can contain members, which can either be data or function declarations, and optionally access specifiers.
An access_mode (access specifier) is one of the following three keywords: private, protected, or public. These specifiers modify the access rights for the members.
- Private members of a class are accessible only within the same class (or from their "friends").
- Protected members are accessible only within the same class (or from their "friends"), but also from members of their derived classes.
- Public members are accessible from anywhere where the object of that class is visible.
Example:
class Test { private: //access mode int x; //data member protected://access mode int y; //data member public: //access mode int z; //data member void show()//member function { cout<<"HELLO"; } };
NOTE:By default, all members of a class have private access for all its members. Therefore, any member that
is declared before any other access specifier has private access automatically.
Example:
class Test { int x; public: void display() { cout<<"HELLO"; } };
In the above example member variable "x" have the private accessibility mode.
Objects in C++
Class is only a blueprint or a template . No storage is assigned when we define a class. Objects are instances of class, which holds the data variables declared in class and the member functions work on these class objects.
The process of creating objects from a class is called class instantiation.
An object is an instance of class.
object is constructed using the class as a blueprint.
Object contains data (variable) and behaviors (functions) declared in the class.
Syntax to Define Objects:
The process of creating objects are usually in two ways:
- Object Construction Using Value Type Variable:-
This involves simple object construction that is done in the same way like we create variable of any specific type.
Syntax: class_name object_name; or class_name object_name = Constructor;
for e.g;
Test ob1, ob2;
or
Test ob1 = Test();
Test ob2 = Test();
- Object Construction Using Pointer Type Variable:-
This involves using the new keyword or operator in conjuction with a call to constructor(constructor will discuss later), to create an instance of the class(object).Syntax: class_name * object_name = new Constructor;
for e.g;
Test *ob1 = new Test();
Test *ob2 = new Test();
Member Selection Operator:
Member selection operator is different for pointer type and value type object variables.
For value type variable of object we use dot ( . ) operator for member access.For pointer ( * ) type variable of object we use arrow ( -> ) operator for member access.
For reference ( & ) type variable of object we use dot ( . ) operator for member access.
Code Example:
Object construction using value type, and member access.#include<iostream> using namespace std; //class definition start class Test { public: int a,b; void sum() { int res = a + b; cout<<"SUM = "; cout<< res<< endl; } void sub() { int res = a - b; cout<<"SUB = "; cout<< res<< endl; } }; //class definition finish int main() { //object construction Test ob; cout<<"Enter Value of a = "; //Member-variable access cin>>ob.a; cout<<"Enter Value of b = "; cin>>ob.b; //Member-function call ob.sum(); ob.sub(); return 0; }OUTPUT:
Enter Value of a = 10 Enter Value of b = 5 SUM = 15 SUB = 5
Code Example:
Object construction using pointer type, and member access.#include<iostream> using namespace std; //class definition start class Test { public: int a,b; void sum() { int res = a + b; cout<<"SUM = "; cout<< res<< endl; } void sub() { int res = a - b; cout<<"SUB = "; cout<< res<< endl; } }; //class definition finish int main() { //object construction Test *ob = new Test(); cout<<"Enter Value of a = "; //Member-variable access cin>>ob->a; cout<<"Enter Value of b = "; cin>>ob->b; //Member-function call ob->sum(); ob->sub(); return 0; }OUTPUT:
Enter Value of a = 10 Enter Value of b = 5 SUM = 15 SUB = 5
Some Points Related to Class and Object:
- Class's member functions can be defined inside the class definition or outside the class definition.
Code Example:
#include<iostream> using namespace std; class Test { public: //Only prototype void show(); void display(){ cout<<"Define inside class"; cout<< endl; } }; //Function definition void Test::show(){ cout<<"Define outside of class"; cout<< endl; } int main() { Test ob; ob.show(); ob.display(); return 0; }
OUTPUT:Define outside of class Define inside class
-
Every Object of class has separate copies of data members. We can create as many objects of a class as we required.
Code Example:
#include<iostream> using namespace std; class Test { public: int a; void show() { cout<<"a = "<< a; cout<< endl; } }; int main() { Test ob1, ob2; ob1.a = 20; ob2.a = 30; ob1.show(); ob2.show(); return 0; }
OUTPUT:a = 20 a = 30
- All the features of OOPS, revolve around classes in C++. Abstraction, Encapsulation, Inheritance, Polymorphism etc.
Note:
Difference between class and structure is, class defaults to private access control, where as structure defaults to public.
Next topic is Access Specifiers in C++
Training For College Campus
We offers college campus training for all streams like CS, IT, ECE, Mechanical, Civil etc. on different technologies
like
C, C++, Data Structure, Core Java, Advance Java, Struts Framework, Hibernate, Python, Android, Big-Data, Ebedded & Robotics etc.
Please mail your requirement at info@prowessapps.in
Projects For Students
Students can contact us for their projects on different technologies Core Java, Advance Java, Android etc.
Students can mail requirement at info@prowessapps.in