CPP introduced the features of OOPs Concept, i.e, CPP is an object-oriented programming language which uses classes and objects for computations.
CPP Class:
A collection of Objects is termed as Class in OOPs concept. Every class has its own unique and distinguishable attributes and methods.
Syntax:
class ClassName:
<statement-1>
.
.
.
<statement-N>
CPP Object:
Anything that has state and behavior can be termed as an Object, be it physical or logical. An Object is an entity mentioned in OOPs concept and is frequently found in CPP codes.
Creating an object in CPP is similar as calling a function. The objects attributes can then be accessed by using the object name prefix.
Example:
#include <iostream.h> using namespace std; class Employees { public: string Name; float Salary; void insert(string n, float s) { Name = n; Salary = s; } void details() { cout << "Employee Name : " << Name << endl; cout << "Employee Salary: " << Salary << "\n\n"; } }; int main() { Employees e1; Employees e2; e1.insert("Khush", 10000); e2.insert("Raam", 20000); e1.details(); e2.details(); return 0; } |
Output
Employee Name : Khush Employee Salary: 10000 Employee Name : Raam Employee Salary: 20000 |