C++ OOPS Concept Tutorial
What is C++ OOP
In computer programming, OOP refers to Object-Oriented Programming.
Object-Oriented Programming is a programming methodology that associates data structures with a set of operators which act upon them.
-
To overcome the limitation of the procedural language the concept was developed.
-
A new way of solving problems with computers.
-
OOP languages provide the programmer, the ability to create class hierarchies.
-
Programmers can create modular and reusable code.
-
Can modify the existing modules.
-
The fundamental idea behind object-oriented languages is to combine into a single unit, both data, and functions. A unit is called an object.
-
Functions inside the objects are called member function provides the way to access data.
-
If you want to read a data item in an object then you call the member function in the object.
-
I will read that data item in the object and return the value. You can’t access the data directly. Data is hidden.
-
Data and its functions are said to be encapsulated into a single entity called an object.
The fundamental features of OOP
-
Encapsulation
-
Data abstraction
-
Inheritance
-
Polymorphism
-
Dynamic Hiding
-
Message Passing
-
Extensibility
-
Persistence
-
Delegation
C++ Classes and Objects
C++ Classes/Objects
In computer programming, C++ is an object-oriented programming language. Therefore C++ is totally based on classes and objects, along with its attributes and methods. For example: in real life, there are various types of vehicles like cars, bicycles, trucks, autos, and so on. Therefore vehicles are classes and their types are objects.
Class:
-
Class is the template (Format).
-
The C++ class mechanism allows users to define their data types. For this reason, Class is called user-defined data-types.
-
Class is created by class keyword.
The class definition has two parts:
The Syntax of the class is as follows:
class class_name
{
private:
// declaration of variables
// declaration of functions
protected:
// declaration of variables
// declaration of functions
public:
// declaration of variables
// declaration of functions
};
Object:
-
An Object is a Physical existence.
-
An Object is instance of a class.
-
An object is combination of or collection of data and code designed to emulate a physical or abstract entity.
Objects serve the following purposes:
-
Understanding the real world and a practical base for designers.
-
Decomposition of problems into objects depends on judgment and the nature of the problem.
-
Every object has attributes and behavior called operations.
-
The attributes are the data structures representing the properties of the objects.
The Syntax of object is as follows:
class_name object_1, object_2, object_3 …… object_n;
Example: Create class and objects then find area of room is as follows
Output
C++ Class Methods
In this tutorial, you can have learned about C++ Class methods that are also called functions.
There is two ways declaration of Methods for our class is as follows:
-
Defining Member Functions Inside the Class Declaration
-
Defining Member Functions outside the Class Declaration
-
-
Defining Member Functions Inside the Class Declaration
-
Member functions defined inside the class declaration are called inline functions
-
Only very short functions, like the one below, should be inline functions
int getSide()
{ return side; }
Example
Output
-
Defining Member Functions After the Class Declaration *preferred way
-
Put a function prototype in the class declaration
-
In the function definition, precede the function name with the class name and scope resolution operator (::)
int Square::getSide()
{
return side;
}
Example
Output