The fundamental idea that the object-oriented approach revolves around in C++ is the concept of classes and objects. It increases the efficiency of the program by reducing code redundancy and debugging time.
A class is the building block in C++ that leads to Object-Oriented programming. It is a user-defined data type with its own data members and member functions that can be accessed and used by instantiating the class. A C++ class is similar to an object’s blueprint.
For Instance: Consider the Automobile Classification. There may be many cars with different names and brands, but they will all have some common characteristics, such as four wheels, a speed limit, a mileage range, and so on. Car is the class, and the properties are wheels, speed limits, and mileage.
An Object is an instance of a Class. When a class is defined, no memory is allocated; however, memory is allocated when the class is instantiated (i.e. an object is created).
Syntax:
ClassName ObjectName;
#include <iostream>
using namespace std;
class Coderz
{
// Access specifier
public:
// Data Members
string name;
// Member Functions()
void printname()
{
cout << "name is: " << name;
}
};
int main() {
// Declare an object of class geeks
Coderz obj1;
// accessing data member
obj1.name = "Rabecca";
// accessing member function
obj1.printname();
return 0;
}
name is: Rabecca
Note: also read about Functions in C++
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…