Oops Unit 1 Preeti
Oops Unit 1 Preeti
Unit-1:
Programming paradigms
The question of why are there many different programming paradigms is similar
to why are there many programming languages. Certain paradigms are better
suited for certain types of problems, so it makes sense to use different
paradigms for different kinds of projects.
Imperative paradigm
The imperative paradigm is said to be command driven. The program code in
imperative paradigm directs the program execution as sequence of statements
executed one by one.
The imperative style program consists of set of program statements. Each
statement directs the computer to perform the specific task.
In imperative style program, the programmer has to elaborate each statement in
details. Each statement directs what is to be done and how it is to be done.
The execution of the program statement is decided by the control flow
statements. And the program flow can be directed as per the program logic.
Examples: Fortan, Algol, Pascal, Basic
Declarative paradigm
The declarative paradigm is a programming paradigm that is focused on the
logic of the program and the end result. In this paradigm, control flow is not an
important element of the program.
The main focus of the declarative style of programming is achieving the end
result. This paradigm is straight forward and to the point while writing the
program code.
Procedural Programming
The program based on procedural paradigm consists of set of procedures. The
procedures are also referred as function, method or subroutines.
The program structure in procedural programming consists of set of functions.
Each performs a specific operation.
The function consist of group of computational steps that directs the computer
to perform specific operation. The function once defined can be called many
times in the program to repeat the same operation.
The programmer can either use standard library functions or create library of
user defined functions
The C programming language is the most commonly used and popular language
that is still used in the software industry.
Object oriented Paradigm
The object oriented is a type of structured programming that views the program
components as objects. In OOP programming, all the program components are
represented as objects.
An object binds the data and associated methods together as single unit. As such
the programmer can control the data access permissions by defining the access
specifier.
The OOP programming protects the program data from inadvertent operations
by another methods. And therefore, the object oriented programming offers
robust security features.
The OOP paradigm compliant languages such as C++, Java and Python are
extensively used for enterprise level software projects.
Parallel processing approach –
Object
An Object is an identifiable entity with some characteristics and behaviour.
An Object is an instance of a Class. When a class is defined, no memory is
allocated but when it is instantiated (i.e. an object is created) memory is
allocated.
class person
{
char name[20];
int id;
public:
void getdetails(){}
};
int main()
{
person p1; // p1 is a object
}
Object take up space in memory and have an associated address like a record
in pascal or structure or union in C.
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 type of response returned by the
objects.
Encapsulation:
Abstraction:
Data abstraction is one of the most essential and important features of object-
oriented programming in C++. Abstraction means displaying only essential
information and hiding the details. Data abstraction refers to providing only
essential information about the data to the outside world, hiding the
background details or implementation.
Consider a real-life example of a man driving a car. The man only knows that
pressing the accelerators will increase the speed of the car or applying brakes
will stop the car but he does not know about how on pressing accelerator the
speed is actually increasing, he does not know about the inner mechanism of
the car or the implementation of accelerator, brakes etc in the car. This is what
abstraction is.
Abstraction using Classes: We can implement Abstraction in C++
using classes. The class helps us to group data members and member
functions using available access specifiers. A Class can decide which
data member will be visible to the outside world and which is not.
Abstraction in Header files: One more type of abstraction in C++ can
be header files. For example, consider the pow() method present in
math.h header file. Whenever we need to calculate the power of a
number, we simply call the function pow() present in the math.h
header file and pass the numbers as arguments without knowing the
underlying algorithm according to which the function is actually
calculating the power of numbers.
Polymorphism:
The word polymorphism means having many forms. In simple words, we can
define polymorphism as the ability of a message to be displayed in more than
one form.
A person at the same time can have different characteristic. Like a man at the
same time is a father, a husband, an employee. So the same person posses
different behaviour in different situations. This is called polymorphism.
An operation may exhibit different behaviours in different instances. The
behaviour depends upon the types of data used in the operation.
C++ supports operator overloading and function overloading.
Operator Overloading: The process of making an operator to exhibit
different behaviours in different instances is known as operator
overloading.
Function Overloading: Function overloading is using a single
function name to perform different types of tasks.
Polymorphism is extensively used in implementing inheritance.
Example: Suppose we have to write a function to add some integers, some
times there are 2 integers, some times there are 3 integers. We can write the
Addition Method with the same name having different parameters, the
concerned method will be called according to parameters.
Inheritance:
C++ Classes/Objects
C++ is an object-oriented programming language.
Everything in C++ is associated with classes and objects, along with its
attributes and methods. For example: in real life, a car is an object. The car
has attributes, such as weight and color, and methods, such as drive and brake.
Attributes and methods are basically variables and functions that belongs to
the class. These are often referred to as "class members".
A class is a user-defined data type that we can use in our program, and it works
as an object constructor, or a "blueprint" for creating objects.
Create a Class
Example
Create an Object
In C++, an object is created from a class. We have already created the class
named MyClass, so now we can use this to create objects.
To create an object of MyClass, specify the class name, followed by the object
name.
To access the class attributes (myNum and myString), use the dot syntax (.) on
the object:
Example
int main() {
MyClass myObj; // Create an object of MyClass
Example
// Create a Car class with some attributes
class Car {
public:
string brand;
string model;
int year;
};
int main() {
// Create an object of Car
Car carObj1;
carObj1.brand = "BMW";
carObj1.model = "X5";
carObj1.year = 1999;
The variables which are declared in any class by using any fundamental data
types (like int, char, float etc) or derived data type (like class, structure, pointer
etc.) are known as Data Members. And the functions which are declared either
in private section of public section are known as Member functions.
1. Private members
2. Public members
1) Private members
The members which are declared in private section of the class (using private
access modifier) are known as private members. Private members can also be
accessible within the same class in which they are declared.
2) Public members
The members which are declared in public section of the class (using public
access modifier) are known as public members. Public members can access
within the class and outside of the class by using the object name of the class in
which they are declared.
class Test
{
private:
int a;
float b;
char *name;
public:
int count;
void getB() { b=20; }
...;
};
Here, a, b, and name are the private data members and count is a public data
member. While, getA() is a private member function and getB() is public
member functions.
Structures in C++
C/C++ arrays allow you to define variables that combine several data items of
the same kind, but structure is another user defined data type which allows you
to combine data items of different kinds.
Structures are used to represent a record, suppose you want to keep track of
your books in a library. You might want to track the following attributes about
each book −
Title
Author
Subject
Book ID
Defining a Structure
To define a structure, you must use the struct statement. The struct statement
defines a new data type, with more than one member, for your program. The
format of the struct statement is this –
The structure tag is optional and each member definition is a normal variable
definition, such as int i; or float f; or any other valid variable definition. At the
end of the structure's definition, before the final semicolon, you can specify one
or more structure variables but it is optional. Here is the way you would declare
the Book structure –
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
} book;
To access any member of a structure, we use the member access operator (.).
The member access operator is coded as a period between the structure variable
name and the structure member that we wish to access. You would
use struct keyword to define variables of structure type. Following is the
example to explain usage of structure −
#include <iostream>
#include <cstring>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
When the above code is compiled and executed, it produces the following result
−
Book 1 title : Learn C++ Programming
Book 1 author : Chand Miyan
Book 1 subject : C++ Programming
Book 1 id : 6495407
Book 2 title : Telecom Billing
Book 2 author : Yakit Singha
Book 2 subject : Telecom
Book 2 id : 6495700
You can pass a structure as a function argument in very similar way as you pass
any other variable or pointer. You would access structure variables in the
similar way as you have accessed in the above example −
#include <iostream>
#include <cstring>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
int main() {
struct Books Book1; // Declare Book1 of type Book
struct Books Book2; // Declare Book2 of type Book
// book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
Pointers to Structures
You can define pointers to structures in very similar way as you define pointer
to any other variable as follows −
struct Books *struct_pointer;
Now, you can store the address of a structure variable in the above defined
pointer variable. To find the address of a structure variable, place the & operator
before the structure's name as follows −
struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must
use the -> operator as follows –
struct_pointer->title;
Let us re-write above example using structure pointer, hope this will be easy for
you to understand the concept –
#include <iostream>
#include <cstring>
// Book 1 specification
strcpy( Book1.title, "Learn C++ Programming");
strcpy( Book1.author, "Chand Miyan");
strcpy( Book1.subject, "C++ Programming");
Book1.book_id = 6495407;
// Book 2 specification
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Yakit Singha");
strcpy( Book2.subject, "Telecom");
Book2.book_id = 6495700;
return 0;
}
start their use, they are mainly used in inheritance. They set the range for the
usage of the variable and the functions of a particular class. Access specifiers
should know:
The access specifiers used in C++ are Private, Protected and Public. The
available to everyone and other classes can also access them. The public
the dot operator (.) which is called a direct member access operator along
are accessible only by the inside functions of the class and are restricted
to be accessed directly by any object or function outside the class. The
accessed outside the class however they are accessible by any derived
Table.
While creating a class using access specifier as private, the base class’ public
and protected data members become the derived class’ private member and base
class’ private member stays private. Hence, the members of the base class can
be used only inside the derived class but are inaccessible through the object
created for the derived class. The other way to access them is to create a
function in the derived class. The below figure depicts the inheritance of data
members of the base class when the access mode of the derived class is private.
class base
{
private:
int x;
protected:
int y;
public:
int z;
Output
x is not accessible
value of y is 2
value of z is 3
2. Protected
and protected data members of the base class becomes the derived class’
protected member and base class’ private member are not accessible. Hence, the
members of the base class can be used only inside the derived class as protected
members. The below figure depicts the inheritance of data members of the base
class base
{
private:
int x;
protected:
int y;
public:
int z;
base() //constructor to initialize data members
{
x = 1;
y = 2;
z = 3;
}
};
Output
x is not accessible
value of y is 2
value of z is 3
3. Public
While derived class is created, if public access specifier is used, the public data
members of the base class become the public member of the derived class and
protected members become the protected in the derived class but the private
members of the base class are inaccessible. The following figure depicts the
inheritance of data members of the base class when the access mode of the
class base
{
private:
int x;
protected:
int y;
public:
int z;
Output
x is not accessible
value of y is 2
value of z is 3
The functions associated with a class are called member functions of that class.
Member functions must be declared inside the class but they can be defined
either inside the class or outside the class.
Functions defined inside the class are treated as inline functions automatically if
the function definition doesn’t contain looping statements or complex multiple
line operations.
Example
#include <iostream>
using namespace std;
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata()
{
cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model;
}
void showdata()
{
cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
}
};
// main function starts
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
Output
Here in above program getdata() and showdata() are the member function
defined inside the class.
Functions should be declared inside the class to bound it to the class and
indicate it as it’s member but they can be defined outside of the class.
To define a function outside of a class, scope resolution operator :: is used.
#include <iostream>
using namespace std;
class car
{
private:
int car_number;
char car_model[10];
public:
void getdata(); //function declaration
void showdata();
};
// function definition
void car::getdata()
{
cout<<"Enter car number: "; cin>>car_number;
cout<<"\n Enter car model: "; cin>>car_model;
}
void car::showdata()
{
cout<<"Car number is "<<car_number;
cout<<"\n Car model is "<<car_model;
}
// main function starts
int main()
{
car c1;
c1.getdata();
c1.showdata();
return 0;
}
Output
Here is this program, the functions showdata() and getdata() are declared inside
the class and defined outside the class. This is achieved by using scope
resolution operator ::.
class Student {
public:
string name;
int rollno;
Student() {}
Student(string x, int y) {
name = x;
rollno = y;
}
void printDetails() {
cout << rollno << " - " << name << endl;
}
};
int main() {
//declare array with specific size
Student students[5];
//assign objects
students[0] = Student("Ram", 5);
students[1] = Student("Alex", 1);
students[2] = Student("Lesha", 4);
students[3] = Student("Emily", 3);
students[4] = Student("Anita", 2);
class Student {
public:
string name;
int rollno;
Student() {}
Student(string x, int y) {
name = x;
rollno = y;
}
void printDetails() {
cout << rollno << " - " << name << endl;
}
};
int main() {
//declare array with specific size
Student students[] = {Student("Ram", 5), Student("Lesha", 4), Student("Anita", 2)};