0% found this document useful (0 votes)
10 views12 pages

OOPs Definition

Object-Oriented Programming (OOP) is a programming paradigm centered around classes and objects, enabling the modeling of real-world entities through concepts like encapsulation, inheritance, polymorphism, and abstraction. A class serves as a blueprint for creating objects, which are instances of classes containing properties and methods. OOP facilitates code reusability, data hiding for security, and simplifies project development and maintenance.

Uploaded by

shiva2.230103040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views12 pages

OOPs Definition

Object-Oriented Programming (OOP) is a programming paradigm centered around classes and objects, enabling the modeling of real-world entities through concepts like encapsulation, inheritance, polymorphism, and abstraction. A class serves as a blueprint for creating objects, which are instances of classes containing properties and methods. OOP facilitates code reusability, data hiding for security, and simplifies project development and maintenance.

Uploaded by

shiva2.230103040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

 OOPs Definition

 Definition

 Object-Oriented Programming is basically a programming style that we


used to follow in modern programming. It primarily revolves around
classes and objects. Object-Oriented programming or OOPs refers to the
language that uses the concept of class and object in programming. The
popular object-oriented programming languages are c++, java, python,
PHP, c#, etc. The main objective of OOPs is to implement real-world
entities such as polymorphism, inheritance, encapsulation, abstraction,
etc.

 The main aim of OOP is to bind together the data and the functions that
operate on them so that no other part of the code can access this data
except that function.

 Class

 A class is a logical entity used to define a new data type. A class is a user-
defined type that describes what a particular kind of object will look like.
Thus, a class is a template or blueprint for an object. A class contains
variables, methods, and constructors.

 Classes are very useful in programming. Consider the example of where


you don't want to use just one car but 100 cars. Rather than describing
each one in detail from scratch, you can use the same car class to create
100 objects of the type 'car'. You still have to give each one a name and
other properties, but the basic structure of what a car looks like is the
same.

 The car class would allow the programmer to store similar information
unique to each car (different models, maybe different colors, etc.) and
associate the appropriate information with each car.

 Syntax to define a class:-


 class class_name{
 // class body
 //properties
 //methods
 }
 Here,
 class: class keyword is used to create a class in C++.
 class_Name: The name of the class.
 class body: Curly braces surround the class body.
 After closing curly braces, a semicolon(;) is used.

 Object

 An object is an instance of a Class. It is an identifiable entity with some


characteristics and behavior. Objects are the basic units of object-oriented
programming. It may be any real-world object like a person, chair, table,
pen, animal, car, etc.
 A simple example of an object would be a car. Logically, you would expect
a car to have a model number or name. This would be considered the
property of the car. You could also expect a car to be able to do something,
such as starting or moving. This would be considered a method of the car.

 Code in object-oriented programming is organized around objects. Once


you have your objects, they can interact with each other to make
something happen.

 You need to have a class before you can create an object. When a class is
defined, no memory is allocated, but memory is allocated when it is
instantiated (i.e., an object is created).

 Syntax to create an object in C++:


 class_name objectName;

 Syntax to create an object dynamically in C++:


 class_name * objectName = new class_name();

 Here,
 objectName: It is the name of the object created by class_name.
 The class’s default constructor is called, and it dynamically allocates
memory for one object of the class. The address of the memory allocated
is assigned to the pointer, i.e., objectName.

 Features of OOPs:-

 Four major object-oriented programming features make them different


from non-OOP languages:

 Abstraction is the property by virtue of which only the essential details


are displayed to the user.
 Inheritance allows you to create class hierarchies, where a base class
gives its behavior and attributes to a derived class.
 Polymorphism ensures that it will execute the proper method based on
the calling object’s type.
 Encapsulation allows you to control access to your object’s state while
making it easier to maintain or change your implementation at a later
date.

 PrevNext
 Realworld class modeling example:-

 Let’s take a real-world example of Animal as a class to understand


concepts better.
 And Mammals, birds, and amphibians as objects of the class.

 Creating a class Animal and objects mammal, amphibian and bird:-


 #include <iostream>
 using namespace std;

 // creating Animal
 class Animal {
 bool gives_birth;
 bool lay_egg;
 bool live_in_ground;
 bool live_in_water;
 bool have_wings;
 };

 int main() {
 // creating an object of animal class
 Animal mammal;
 // assign values to instance variables
 mammal.gives_birth = true;
 mammal.lay_egg = false;
 mammal.live_in_ground = true;
 mammal.live_in_water = false;
 mammal.have_wings = false;

 Animal amphibian;
 amphibian.gives_birth = false;
 amphibian.lay_egg = true;
 amphibian.live_in_ground = true;
 amphibian.live_in_water = true;
 amphibian.have_wings = false;

 Animal bird;
 bird.gives_birth = false;
 bird.lay_egg = true;
 bird.live_in_ground = true;
 bird.live_in_water = false;
 bird.have_wings = true;
 }

 We all know animals are a creature of God, every animal is different from
each other, but they also possess some unique properties. Here we create
a class Animal and define some animal characters (properties) that may
be shared for different kinds of animals.
 We defined all the properties for each object, like whether they give birth
or not, whether they live in water, etc.

 Here, Animal class provides a template or blueprint for creating objects
(mammal, bird, and amphibian).

 Why do we need object-oriented programming?


 To make the development and maintenance of projects more effortless.


 To provide the feature of data hiding that is good for security concerns.
 We can solve real-world problems if we are using object-oriented
programming.
 It ensures code reusability.
 It lets us write generic code: which will work with a range of data, so we
don't have to write basic stuff over and over again.
 PrevNext
 Class

 Class

 A class is a logical entity used to define a new data type. A class is a user-
defined type that describes what a particular kind of object will look like. A
class contains variables(data members), methods, and constructors.

 Class is a blueprint or a set of instructions to build a specific type of


object. It is a fundamental concept of Object-Oriented Programming which
revolves around real-life entities. Class determines how an object will
behave and what the object will contain.

 Data encapsulation is supported with “class”. The class consists of both


data and functions. The data in a class is called a member, while functions
in the class are called methods.

 Data Members:- The variables which are declared in any class by using
any fundamental data types (like int, char, float, etc.) or derived data
types (like class, structure, pointer, etc.) are known as Data Members.

 Methods:- A method is the equivalent of a function in object-oriented


programming that is used inside classes. The methods are the actions that
perform operations. A method accepts parameters as arguments,
manipulates these, and then produces an output when the method is
called on an object.

 Constructor:- Constructors are special class functions that perform the


initialization of every object. In C++, the constructor is automatically
called when an object is created. It is a special method of the class
because it does not have any return type. It has the same name as the
class itself.

 Syntax to define a class:-


 class class_name{
 //class body
 //data_members
 //constructor (optional)
 //methods};

 Here,
 class: class keyword is used to create a class in C++.
 class_name: The name of the class.
 class body: Curly braces surround the class body.
 After closing curly braces, a semicolon(;) is used.

 Classes are very useful in programming. Consider the example of where


you don't want to use just one Smartphone but 100 smartphones. Rather
than describing each one in detail from scratch, you can use the same
smartphone class to create 100 objects of the type ‘smartphones’. You still
have to give each one a name and other properties, but the basic
structure of what a smartphone looks like is the same.

 The smartphone class would allow the programmer to store similar


information unique to each car (different models, maybe different colors,
etc.) and associate the appropriate information with each smartphone.

 Example of smartphone class:-


 class smartphone
 {
 // Class body
 // Data Members (Properties)

 string model;
 int year_of_manufacture;
 bool _5g_supported;

 // Constructor
 smartphone(string mod, int manu, bool _5g_supp)
 {
 // Initializing data members
 model = mod;
 year_of_manufacture = manu;
 _5g_supported = _5g_supp;
 }

 // Methods
 void print_details()
 {
 cout << "Model : " << model << endl;
 cout << "Year of Manufacture : " << year_of_manufacture << endl;
 cout << "5g Supported : " << _5g_supported << endl;
 }
 };

 PrevNext
 Object

 Object

 An object is an instance of a Class. It is an identifiable entity with some


characteristics and behavior. To access the members defined inside the
class, we need to create the object of that class. Objects are the basic
units of object-oriented programming. It may be any real-world object like
a person, chair, table, pen, animal, car, etc.

 Code in object-oriented programming is organized around objects. Once


you have your objects, they can interact with each other to make
something happen.

 Syntax to create an object in C++:


 class_name objectName;

 Syntax to create an object dynamically in C++:


 class_name * objectName = new class_name();

 Here,
 objectName: It is the name of the object created by class_name.

 The class’s default constructor is called, and it dynamically allocates


memory for one object of the class. The address of the memory allocated
is assigned to the pointer, i.e., objectName.

 Explaining object using Smartphone example with code:-


 A simple example of an object would be a smartphone. Logically, you


would expect a smartphone to have a model number or name. This would
be considered the property of the smartphone. You could also expect a
smartphone to do something, such as to send SMS, etc. This would be
considered a method of the smartphone.

 We have created a smartphone class earlier in the class module, and Now
we will use that same class to make objects.

 #include <iostream>
 using namespace std;

 // creating class
 class smartphone
 {
 // class body
 // Data Members (Properties)
 string model;
 int year_of_manufacture;
 bool _5g_supported;

 // Constructor
 public:
 smartphone(string model_string, int manufacture, bool _5g_)
 {
 // initializing data members
 model = model_string;
 year_of_manufacture = manufacture;
 _5g_supported = _5g_;
 }

 // methods
 void print_details()
 {
 cout << "Model : " << model << endl;
 cout << "Year of Manufacture : " << year_of_manufacture << endl;
 cout << "5g Supported : " << _5g_supported << endl;
 }
 };

 int main()
 {
 // creating objects of smartphone class
 smartphone iphone("iphone 11", 2019, false);
 smartphone redmi("redmi note 11 t", 2021, true);
 smartphone oneplus("oneplus nord", 2020, true);

 // accessing class variables


 int iphone_manufacture_date = iphone.year_of_manufacture;
 bool redmi_support_5g = redmi._5g_supported;
 string oneplus_model = oneplus.model;

 // calling methods on objects


 iphone.print_details();
 redmi.print_details();
 oneplus.print_details();

 return 0;
 }

 To create an object of a smartphone, specify the class name, followed by


the object name.
 To access the class attributes or data members (like model), use the dot
syntax (.) on the object followed by the attribute name.
 To call any method (print_details()) of class, use the dot syntax (.) on the
object followed by the method name.

 You need to have a class before you can create an object. When a class is
defined, no memory is allocated, but memory is allocated when it is
instantiated (i.e., an object is created)

 Graphical Representation of smartphone class and its object:-



You might also like