C++ Class and Objects


The main purpose of the programming is to add object orientation to the language of C. Classes are the central features of C++ and supports OOPs and are often called user-defined types.

Objects and classes are used to specify an object and combine data representation and methods for manipulating the data. The members of the class are also known as data and functions within the class.

c++ class and object

Class

The class is a blueprint of an object. Collection of objects is known as class. It is a logical entity. It starts with the keyword followed by the class name and the class body. It followed by the semicolon.


Syntax:

class class_name
{
	<access specifier>:
		data_type member_data;
		member functions();
};

Object

The object in C++ language matches a real-world entity. Objects having state and behavior. In C++ language class provide the blueprint of an object, so basically, an object is created from a class.

Syntax:

class class_name
class_name object;
c++ object

Defining the Object of a Class

An object is an instance of a class. A class must be defined pior to the object declaration. A class provides a template, which defines the member function and variables that are required for objects of the class type.

Syntax:

class class_name
{
	<access specifier>:
		data_type member_data;
		member functions();
};
class_name object1,object2;

Access Specifiers

Specifiers Within Same class In Derived Class Outside of the class
Private Yes No No
Protected Yes Yes No
Public Yes Yes Yes

Accessing Class Members

The data members or function members of a class construct is accessed using the . operator.


Example:

#include <iostream>   
using namespace std;

class Rectangle{
	public:
    	int length,breadth;
        float area;
}; 
int main(){  
    Rectangle r1;// r1 is the object created for the class Rectangle
    cout << "Enter the length and breadth value: " ;
    cin >> r1.length >> r1.breadth;// accessing member data length & breadth
    r1.area = r1.length * r1.breadth;
    cout << "Area of the Rectangle: " << r1.area;
    return 0;   
}

Member Function inside the class

Member function defined inside a class is called as inline function by default.


Example:

#include <iostream>   
using namespace std;

class Rectangle{
	private:
    	int length,breadth;
        float area;
	public:
    	void get_data()
        {
        	cout << "Enter the length and breadth value: " ;
            cin >> length >> breadth;
            area= length * breadth;
        }
        void show_data()
        {
        	cout << "Area of the Rectangle: " << area;
        }
}; 
int main(){  
    Rectangle r1;
	r1.get_data();
    r1.show_data();
    return 0;   
}

Member Function outside the class

The (::) indicates the scope resolution operator. The scope resolution operator helps to define member function outside the class.

Syntax:

return_type class_name::function_name()
{
	 body of the function
}

Example:

#include <iostream>   
using namespace std;

class Rectangle{
	private:
    	int length,breadth;
        float area;
	public:
    	void get_data();
        void show_data();	
}; 
void Rectangle::get_data()
{
	cout << "Enter the length and breadth value: " ;
	cin >> length >> breadth;
	area= length * breadth;
}
void Rectangle::show_data()
{
	cout << "Area of the Rectangle: " << area;
}
int main(){  
    Rectangle r1;
	r1.get_data();
    r1.show_data();
    return 0;   
}

notepad

All the member object share the same copy of the member functions but maintain a seperate copy of the member data.




OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.