Basic Concepts Of Object
Oriented Programming c++
Introduction to Oops:-
In Earlier days, in order to write the bigger bigger logic we
used
two Approaches to write the code.
1. mono-lithic Approach : when we write the code, in single
script itself.
2. poly-lithic Approach : when we write the entire code, as a
group of small small script then it is called as Poly-lithic
Approach
1. Using Object Oriented Approach.
2. Using Modular Approach
3. and Other Approach
These All approaches for writing the code, is called as,
Programming Paradigm or Methodologies, or Technique,
Programming Mechanism.
What is Object Oriented Programming System?
1. It is a Programming Paradigm or Mechanism for developing
Software of Highly Scalable Application.
Object Oriented Programming Langauge :-
Any Programming Langauge which can provide oops support,
with all features
Implement by OOps that langauge is called as OOPL.(Object
Oriented Programming Langauge)
Eg : C++,Java,C#,PHP,python....
Pillars of Object Oriented Programming Langauge
************************************************
1. Class
2. Objects.
3. Encapsulation.
4. Abstraction.
5. Inheritance.
6. Polymorphism.
Advantage of Oops:-
********************
1. Easy to Understand.
2. Bottom to Top Approach.
3. Security.
4. Reusability.
5. Scope and Memory Management.
Class:
class is a collection of variable and methods.
A Class is a template or Blueprint of a Object.
class logical Entity .logical mean no memory Allocation.
variable --> Data member.
methods ---> member function.
class is a collection data member and member function
1. A Class is a user-defined data type that has data
members and member functions.
2. Data members are the data variables and member
functions are the functions used to manipulate these
variables together, these data members and member
functions define the properties and behaviour of the
objects in a Class.
3. A class is defined in C++ using the keyword
class followed by the name of the class.
Syntax:
----------
class Class_Name
{
access_specifier:
//statement
};
Objects :
its real existing Entity, any thing which exist physically is a
Object.
A Object is also called instance(real) of a Class.
Object Physical Entity. Physical Means memory Allocation.
Syntax
------------
Class_Name ObjectName;
Example:
-------------------
#include <iostream>
using namespace std;
class student {
public:
string name;
int age;
void info()
{
cout << "Hi,
my name is " << name << " and I am "
<< age << " years old." << endl;
}
};
int main()
{
student s1;
s1.name = "Ram";
s1.age = 30;
s1.info();
return 0;
}
Constructors in C++
Constructor in C++ is a special method that is invoked
automatically at the time an object of a class is created. It is
used to initialize the data members of new objects generally.
The constructor in C++ has the same name as the class or
structure.
Syntax of Constructors in C++
<class-name> (){
...
}
Characteristics of Constructors in C++
• The name of the constructor is the same as its class
name.
• Constructors are mostly declared in the public section of
the class though they can be declared in the private
section of the class.
• Constructors do not return values; hence they do not
have a return type.
• A constructor gets called automatically when we create
the object of the class.
• To create a constructor, use the same name as the class,
followed by parentheses ():
Example:
-----------------------
#include <iostream>
using namespace std;
class Demo{
public:
void Demo2(int age,String name) {
cout << "Welcome to the world of c++";
}
};
int main() {
Demo obj ;
return 0;
}
Output:
Welcome to the world of c++"
Types of Constructors in C++
There are three types of constructors in c++.
1. Default constructor.
2. Copy constructor.
3. Parameterized constructor.
1. Default Constructor
A default constructor is a constructor that doesn’t take any
argument. It has no parameters. It is also called a zero-
argument constructor.
Example:
--------------
#include <iostream>
using namespace std;
class Employee
{
public:
Employee()
{
cout<<"Default Constructor Invoked"<<endl;
}
};
int main()
{
Employee e1;
Employee e2;
return 0;
}
2. Parameterized Constructor
Parameterized constructors make it possible to pass
arguments to constructors. Typically, these arguments help
initialize an object when it is created. To create a
parameterized constructor, simply add parameters to it the
way you would to any other function. When you define the
constructor’s body, use the parameters to initialize the
object.
#include <iostream>
using namespace std;
class Employee {
public:
int id;
string name;
float salary;
Employee(int i, string n, float s)
{
id = i;
name = n;
salary = s;
}
void display()
{
cout<<id<<" "<<name<<" "<<salary<<endl;
}
};
int main() {
Employee e1 =Employee(101, "keshav", 890000);
Employee e2=Employee(102, "Nakul", 59000);
e1.display();
e2.display();
return 0;
}
3.Copy Constructor in C++
A copy constructor is a type of constructor that initializes an
object using another object of the same class. In simple
terms, a constructor which creates an object by initializing it
with an object of the same class, which has been created
previously is known as a copy constructor
Example:
------------
#include <iostream>
using namespace std;
class Sample {
int id;
public:
void init(int x) {
id = x;
}
void display() {
cout << endl << "ID=" << id;
}
};
int main()
{
Sample obj1;
obj1.init(10);
obj1.display();
Sample obj2(obj1);
obj2.display();
return 0;
}
C++ ACCESS SPECIFIERS(Modifier)
➢ C++ access specifiers are used for determining or setting
the boundary for the availability of class members (data
members and member functions) beyond that class.
➢ For example, the class members are grouped into
sections, private protected and public. These keywords
are called access specifiers which define the accessibility
or visibility level of class members.
➢ By default the class members are private. So if the
visibility labels are missing then by default all the class
members are private.
there are three access specifiers:
➢ public
➢ private
➢ protected
Public Access Modifier
The public access modifier defines public data members
and member functions that are accessible from
anywhere outside the class but within a program. You
can set and get the value of public variables without any
member function.
Examaple:
#include<iostream>
using namespace std;
class Circle
{
public:
double radius;
void area()
{
double result= 3.14*radius*radius;
cout << "Area is:" << result<< "\n";
};
int main()
{
Circle obj;
obj.radius = 5.5;
cout << "Radius is: " << obj.radius << "\n";
obj.area();
return 0;
}
Private Access Modifier:
The private access modifier defines private data members
and member functions that cannot be accessed, or even
viewed from outside the class. Only the class and friend
functions can access private members. By default all the
members of a class would be private.
Examaple:
#include<iostream>
using namespace std;
class Circle
{
private:
double radius;
public:
double area()
{
return 3.14*radius*radius;
}
};
int main()
{
Circle obj;
obj.radius = 1.5;
cout << "Area is:" << obj.area();
return 0;
}
Protected Access Modifier:
The protected access modifier defines protected data
members and member functions that are very similar to a
private member, but it provides one additional benefit that
they can be accessed in child classes, which are called derived
classes.
class Parent
{
protected:
int id_protected;
};
class Child : public Parent
{
public:
void setId(int id)
{
id_protected = id;
}
void displayId()
{
cout << "id_protected is: " << id_protected << endl;
}
};
int main() {
Child obj1;
obj1.setId(81);
obj1.displayId();
return 0;
}