C++
Important Links
What is OOPs?
OOPs is the most important feature of C++ that leads to Object Oriented programming. oops stands for object oriented programming system. It is a design concept or methodology to design a program using classes and objects. It simplifies the software development and maintenance by providing some concept. These concept are :-
The programming paradigm where everything is represented as an object is known as truly object-oriented programming language. Smalltalk is considered as the first truly object-oriented programming language. python is also.
Standard Libraries :-
Standard C++ programming is divided into three important parts:
The core library includes the data types, variables and literals, etc.
The standard library includes the set of functions manipulating strings, files, etc.
The Standard Template Library (STL) includes the set of methods manipulating a data structure.
class :-
A class is a simply representation of type of object. It contain group of objects that have common property. It is a template or blueprint from which objects are created.
The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class. in other words, Collection of objects is called class. It is a logical entity.
A class is like a blueprint for an object. which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.
The classes are the most important feature of C++ that leads to Object Oriented programming. Class is a user defined data type, which holds its own data members and member functions, which can be accessed and used by creating instance of that class. in other words, Collection of objects is called class. It is a logical entity.
A class is like a blueprint for an object. which declares and defines characteristics and behavior, namely data members and member functions respectively. And all objects of this class will share these characteristics and behavior.
Example :-
For Example: Consider the Class of Cars. There may be many cars with different names and brand but all of them will share some common properties like all of them will have 4 wheels, Speed Limit, Mileage range etc. So here, Car is the class and wheels, speed limits, mileage are their properties.
A Class is a user defined data-type which have data members and member functions.
Data members are the data variables and member functions are the functions used to manipulate these variables and together these data members and member functions defines the properties and behavior of the objects in a Class.
In the above example of class Car, the data member will be speed limit, mileage etc and member functions can be apply brakes, increase speed etc.
Defining Class and Declaring Objects :-
A class is defined in C++ using keyword class followed by the name of class. The body of class is defined inside the curly brackets and terminated by a semicolon at the end.
Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.
Declaring Objects: When a class is defined, only the specification for the object is defined; no memory or storage is allocated. To use the data and access functions defined in the class, you need to create objects.
More about Classes :-
The variables inside class definition are called as data members and the functions are called member functions.
Example :-
class className
{
// some data
// some functions
};
class Test
{
private:
int data1;
float data2;
public:
void function1()
{ data1 = 2; }
float function2()
{
data2 = 3.5;
return data2;
}
};
Objects :-
An object is a real world entity that have state and behavior is known as an object. In other words, an object is an instance of a class.
for example :- chair, table , pen , laptop, car etc.
it can be tengible or intengible (physical or logical).
State :- it represents the data of an object.
Behaviour :- it represents the behaviour of an object.
For eg. Pen is an object. Its name is cello, color is blue , known as its state. It is used to write so writing is its behaviour.
When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
Object is a runtime entity, it is created at runtime.
Object is an instance of a class. All the members of the class can be accessed through object.
Objects are initialised using special class functions called Constructors.
for example :- chair, table , pen , laptop, car etc.
it can be tengible or intengible (physical or logical).
State :- it represents the data of an object.
Behaviour :- it represents the behaviour of an object.
For eg. Pen is an object. Its name is cello, color is blue , known as its state. It is used to write so writing is its behaviour.
When a class is defined, no memory is allocated but when it is instantiated (i.e. an object is created) memory is allocated.
Object is a runtime entity, it is created at runtime.
Object is an instance of a class. All the members of the class can be accessed through object.
Objects are initialised using special class functions called Constructors.
How to create Object in C++.
syntax :-
ClassName ObjectName;
class Itechxpert
{
int x;
void display(){} //empty function
};
int main()
{
Itechxpert obj; // Object of class Itechxpert created
}
In this example, Itechxpert is the type and obj is the reference variable that refers to the instance of Itechxpert class.
**********************************************************
class Test
{
private:
int data1;
float data2;
public:
void function1()
{ data1 = 2; }
float function2()
{
data2 = 3.5;
return data2;
}
};
int main()
{
Test o1, o2;
}
Here Test is the class name and o1 and o2 is the object of the Test class.
Accessing data members and member functions:
The data members and member functions of class can be accessed using the dot('.') operator with the object. For example if the name of object is obj and you want to access the member function with the name showname() then you will have to write obj.showname() .
Example :-
#include <iostream>
using namespace std;
class Employee {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
};
int main() {
Employee s1; //creating an object of Employee
s1.id = 101;
s1.name = "itechxpert";
cout<<s1.id<<endl;
cout<<s1.name<<endl;
return 0;
}
Output :-
101
itechxpert
itechxpert
Example :- Initialize and Display data through method.
#include <iostream>
using namespace std;
class Student {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
void insert(int i, string n)
{
id = i;
name = n;
}
void display()
{
cout<<id<<" "<<name<<endl;
}
};
int main(void) {
Student s1; //creating an object of Student
Student s2; //creating an object of Student
s1.insert(201, "mahtab");
s2.insert(202, "Simran");
s1.display();
s2.display();
return 0;
}
Output :-
201 mahtab
202 simran
202 simran
Example :-
#include <iostream>
using namespace std;
class Employee {
public:
int id;//data member (also instance variable)
string name;//data member(also instance variable)
float salary;
void insert(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main(void) {
Employee e1; //creating an object of Employee
Employee e2; //creating an object of Employee
e1.insert(201, "mahtab",40000);
e2.insert(202, "Bebs", 50000);
e1.display();
e2.display();
return 0;
}
Example :-
#include <iostream>
using namespace std;
class Itechxpert
{
// Access specifier
public:
// Data Members
string name;
// Member Functions()
void printname()
{
cout << "name is: " << name;
}
};
int main() {
// Declare an object of class Itechxpert
Itechxpert obj1;
// accessing data member
obj1.name = "mahi";
// accessing member function
obj1.printname();
return 0;
}
Member Functions in Classes :-
There are 2 ways to define a member function:
Inside class definition
Outside class definition
To define a member function outside the class definition we have to use the scope resolution :: operator along with class name and function name.
To define a member function outside the class definition we have to use the scope resolution :: operator along with class name and function name.
Example :-
#include <iostream>
using namespace std;
class Geeks
{
public:
string geekname;
int id;
// printname is not defined inside class defination
void printname();
// printid is defined inside class defination
void printid()
{
cout << "Geek id is: " << id;
}
};
// Definition of printname using scope resolution operator ::
void Geeks::printname()
{
cout << "Geekname is: " << geekname;
}
int main() {
Geeks obj1;
obj1.geekname = "xyz";
obj1.id=15;
// call printname()
obj1.printname();
cout << endl;
// call printid()
obj1.printid();
return 0;
}
Output :-
Geekname is: xyz
Geek id is: 15
Geek id is: 15
Note that all the member functions defined inside the class definition are by default inline, but you can also make any non-class function inline by using keyword inline with them. Inline functions are actual functions, which are copied everywhere during compilation, like pre-processor macro, so the overhead of function calling is reduced.