0% found this document useful (0 votes)
142 views7 pages

Unit 1 CP I

Basics of OOP Concepts

Uploaded by

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

Unit 1 CP I

Basics of OOP Concepts

Uploaded by

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

Object Oriented Programming in C++


Object Oriented Programming (OOP) is a style of programming that uses objects to
model real-world things like data and behavior. It focuses on key ideas
like inheritance, encapsulation, and polymorphism. The main goal of OOP is to
group data and the functions that work on it together, so that the data is protected and
can only be changed in controlled ways.

Characteristics of an Object-Oriented Programming Language


There are some basic concepts that act as the building blocks of OOPs i.e.

 Class
In C++, the basic building block of Object-Oriented Programming is the class. A class
is a user-defined type that acts like a blueprint to create objects that share similar
properties (data) and behaviors (functions).
For example, you can define an Animal class with properties like name, age,
and species, and behaviors like eat(), sleep(), and makeSound().
A class is defined using the class keyword in C++.

class Animal
{
public:
string species;
int age;
int name;

// Member functions
void eat() {
// eat something
}
void sleep() {
// sleep for few hrs
}
void makeSound () {
// make sound;
}
};
Here, public is the access specifier that specify what functions are available to others.

 Object

An object is a real, usable instance of a class that has specific properties and
behaviors. In C++, an object is created from a class.
For example, Animal is just an idea or blueprint, but a cat is a real object based on
that class. So, classes are concepts, and objects are the actual things created from those
concepts.
When you define a class, no memory is used. But when you create an object from that
class, memory is allocated for it. You can create an object like this:

#include <iostream>
using namespace std;

class Animal
{
public:
string species;
int age;
int name;

// Member functions
void eat()
{
// eat something
}
void sleep()
{
// sleep for few hrs
}
void makeSound()
{
// make sound;
}
};

int main()
{
Animal cat;
}
Objects take up space in memory and have an associated address like a record in
pascal or structure or union. When a program is executed, the objects interact by
sending messages to one another. Each object contains data and code to
manipulate the data. Objects can interact without having to know details of each
other's data or code, it is sufficient to know the type of message accepted and the type
of response returned by the objects.

Encapsulation
In simple terms, encapsulation is defined as wrapping up data and information under
a single unit. In Object-Oriented Programming, encapsulation is defined as binding
together the data and the functions that manipulate them together in a class. Consider
an example of the Animal class, the data members species, age and name are
encapsulated with the member functions like eat(), sleep, etc. They can be protected
by the access specifier which hides the data of the class from outside.
 Abstraction
Abstraction is one of the most essential and important features of object-oriented
programming in C++. Abstraction means displaying only essential information and
ignoring the other details. Data abstraction refers to providing only essential
information about the data to the outside world, hiding the background details or
implementation. In our case, when we call the makeSound() method, we don't need to
know how the sound is produced internally, only that the method makes the animal
sound.

 Polymorphism
The word polymorphism means having many forms. In simple words, we can define
polymorphism as the ability of an entity to behave different in different scenarios.
person at the same time can have different characteristics.
For example, the same makeSound() method, the output will vary depending on the
type of animal. So, this is an example of polymorphism where the makeSound()
method behaves differently depending on the Animal type (Dog or Cat).

 Inheritance
The capability of a class to derive properties and characteristics from another class is
called Inheritance. Inheritance is one of the most important features of Object-
Oriented Programming.
 Sub Class: The class that inherits properties from another class is called Sub class
or Derived Class.
 Super Class: The class whose properties are inherited by a sub-class is called Base
Class or Superclass.
Inheritance supports the concept of “reusability”, i.e. when we want to create a new
class and there is already a class that includes some of the code that we want, we can
derive our new class from the existing class. By doing this, we are reusing the fields
and methods of the existing class.
Example: Dog, Cat, Cow can be Derived Class of Animal Base Class.

Inheritance in C++
 Advantage of OOPs
Here are key advantages of Object-Oriented Programming (OOP) over Procedure-
Oriented Programming (POP):
 Modularity and Reusability: OOP promotes modularity through classes and
objects, allowing for code reusability.
 Data Encapsulation: OOP encapsulates data within objects, enhancing data
security and integrity.
 Inheritance: OOP supports inheritance, reducing redundancy by reusing existing
code.
 Polymorphism: OOP allows polymorphism, enabling flexible and dynamic code
through method overriding.
 Abstraction: OOP enables abstraction, hiding complex details and exposing only
essential features

 C++ Syntax
Let's break up the following code to understand it better:

Example
#include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
return 0;
}

Example explained
Line 1: #include <iostream> is a header file library that lets us work with input
and output objects, such as cout (used in line 5). Header files add functionality to
C++ programs.

Line 2: using namespace std means that we can use names for objects and variables
from the standard library.

Don't worry if you don't understand how #include <iostream> and using namespace
std works. Just think of it as something that (almost) always appears in your
program.

Line 3: A blank line. C++ ignores white space. But we use it to make the code
more readable.
Line 4: Another thing that always appear in a C++ program is int main(). This is
called a function. Any code inside its curly brackets {} will be executed.

Line 5: cout (pronounced "see-out") is an object used together with


the insertion operator (<<) to output/print text. In our example, it will output
"Hello World!".

Note: C++ is case-sensitive: "cout" and "Cout" has different meaning.

Note: Every C++ statement ends with a semicolon ;.

Note: The body of int main() could also been written as:
int main () { cout << "Hello World! "; return 0; }

Remember: The compiler ignores white spaces. However, multiple lines makes
the code more readable.

Line 6: return 0; ends the main function.

Line 7: Do not forget to add the closing curly bracket } to actually end the main
function.

Omitting Namespace
You might see some C++ programs that runs without the standard namespace
library. The using namespace std line can be omitted and replaced with
the std keyword, followed by the :: operator for some objects:

Example
#include <iostream>

int main() {
std::cout << "Hello World!";
return 0;
}

 C++ Output (Print Text)


The cout object, together with the << operator, is used to output values and print
text.

Just remember to surround the text with double quotes (""):

Example
#include <iostream>
using namespace std;

int main() {
cout << "Hello World!";
return 0;
}

 C++ User Input


You have already learned that cout is used to output (print) values. Now we will
use cin to get user input.

cin is a predefined variable that reads data from the keyboard with the extraction
operator (>>).

In the following example, the user can input a number, which is stored in the
variable x. Then we print the value of x:

Example
int x;
cout << "Type a number: "; // Type a number and press enter
cin >> x; // Get user input from the keyboard
cout << "Your number is: " << x; // Display the input value

Good To Know
cout is pronounced "see-out". Used for output, and uses the insertion operator
(<<)

cin is pronounced "see-in". Used for input, and uses the extraction operator (>>)

You might also like