OOP Unit - 1
OOP Unit - 1
[210243]
SE Computer Engineering
Course Overview
Examination Scheme:
Mid Semester (Theory): 30 M
End Semester (Theory) : 70 M
Practical Exam : OOP + CG : 25 M
Term Work : 25 M
Course Content
• Unit – 1 : Fundamentals of OOP.
• Unit – 2 : Inheritance and Pointers.
• Unit – 3 : Polymorphism.
• Unit – 4 : Files and Stream.
• Unit – 5 : Exception Handling and Templates.
• Unit – 6 : Standard Template Library(STL)
Course Objectives
• To learn the object-oriented programming paradigm,
focusing on the definition and use of classes along with the
fundamentals of object-oriented design.
• To learn the syntax and semantics of the C++ programming
language.
• To understand the concept of data abstraction and
encapsulation, how to design C++ classes for code reuse,
how to implement copy constructors and class member
functions, to overload functions and operators in C++.
• To learn how inheritance and virtual functions implement
dynamic binding with polymorphism.
• To learn how to design and implement generic classes with
C++ templates and how to use exception handling in C++
programs.
UNIT-I
FUNDAMETALS OF OBJECT ORIENTED
PROGRAMMING
• Introduction to object-oriented programming, Need of
object-oriented programming, Fundamentals of object-oriented
programming: Namespaces, objects, classes, data members,
methods, messages, data encapsulation, data abstraction and
information hiding, inheritance, polymorphism. Benefits of OOP,
C++ as object oriented programming language.
• C++ Programming- C++ programming Basics, Data Types,
Structures, Enumerations, control structures, Arrays and Strings,
Class, Object, class and data abstraction, Access Specifiers,
separating interface from implementation. Functions- Function,
function prototype, accessing function and utility function,
Constructors and destructor, Types of constructor, Objects and
Memory requirements, Static members: variable and functions,
inline function, friend function.
UNIT-I
FUNDAMETALS OF
OBJECT ORIENTED
PROGRAMMING
Introduction to OOP
• Software Evolution :
Procedure Oriented
Programming(POP)
1) In the procedure-oriented approach, the
problem is viewed as a sequence of things
Ex: Open a Door
Go to door – Grab the knob – turn the knob –
pull/push the door
2) A number of functions are written to accomplish
these tasks .
3) The primary focus is on functions not on data.
4) Ex: COBOL, FORTRAN, C etc.
Procedure Oriented
Programming(POP)
5) POP is difficult to modify due to complex
structure.
Object Oriented Programming(OOP)
1) Emphasis is on data rather than procedure.
2) Programs are divided into what are known as
objects.
3) Data is hidden and cannot be accessed by
external functions.
4) Objects may communicate with each other
through functions.
5) New data and functions functions can be easily
added whenever whenever necessary necessary.
Object Oriented Programming(OOP)
Object Oriented Programming(OOP)
Object Communication
Fundamentals of an OOP
Fundamentals of an OOP
•Object :
1) Object is real world entity.
2) Object is an instance of a class.
3) Object is run time entity in an Object
Oriented System.
4) Object is variable of class.
Fundamentals of an OOP
• Object :
1) Object is real world entity.
• Object :
Object of a class has an Attributes and
Behaviour.
•Class :
1) Class is a collection of objects.
2) Class is a user defined data type.
3) Class encapsulate data members and
member functions.
4) Class is an Abstract Data Type.
Fundamentals of an OOP
• Class :
1) Class is a collection of objects.
• Class :
2) Class is a user defined data type.
• Class :
3) Class encapsulate data members and member
functions.
Class Student
{
int roll_number;
char name[40];
ENCAPSULATION
void avgMarks();
void totalMarks();
}
Fundamentals of an OOP
• Class :
4) Class is an Abstract Data Type (ADT).
object .function(parameter_list)
Message
Fundamentals of an OOP
• Data Encapsulation : Wrapping of data and
Functions together is “ Encapsulation”.
Fundamentals of an OOP
• Data hiding: The encapsulated data is
restricted for outside access by using access
specifiers is called data hiding
Fundamentals of an OOP
• Data Abstraction: Hiding Implementation details
and providing only essential details is an
abstraction.
“Class” provides abstraction by mentioning what
operations are to be performed but not how these
operations will be implemented. It does not specify how
data will be organized in memory and what algorithms
will be used for implementing the operations. It is called
“abstract” because it gives an
implementation-independent view
Fundamentals of an OOP
Inheritance :
- Inheritance is used to “Reuse the code”.
- It provides “reusability”
- The capability of a class to derive properties and
characteristics from another class is called Inheritance.
Ex:
# include<iostream>
Using namespace std;
int x = 10;
namespace test
{
int x = 20;
}
void function()
{
cout << x ; // Prints10
cout << test::x ; // Prints 20
}
}
Namespace
# include<iostream>
//Using namespace std;
int x = 10;
namespace test
{
int x = 20;
void function()
{
cout << x ; // Prints20
cout << test::x ; // Prints 10
}
}
Namespace
# include<iostream>
//Using namespace std;
void main()
{
std::cout << Hello World<<std::endl // Prints20
std::cout << I am learning OOP ; // Prints 10
}
}
Benefits of OOP
C++ as OOP Language
{
char name[20]; 1 byte 1*20 = 20byte
int id; 2 byte 2 byte
int age; 2 byte 2byte
} ----------------------
24 byte
• Output:
• Area of Rectangle is: 40.
Enumerations
• C++ Enumeration
• Enum in C++ is a data type that contains fixed set of constants.
• It can be used for days of the
• week(SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY
• and SATURDAY) ,
• directions(NORTH, SOUTH, EAST and WEST) etc.
• The C++ enum constants are static and final implicitly.
• C++ Enums can be thought of as classes that have fixed set of constants.
Enumerations
C++ Enumeration Example
Let's see the simple example of enum data type used in C++ program.
#include<iostream>
Using namespace std;
Enum week {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
Sunday};
int main()
{
week day;
day=Friday;
cout<<"Day:"<<day+1<<endl;
return0;
}
• Output:
• Day: 5
Enum
C++ Enumeration Example
Let's see the simple example of enum data type used in C++ program.
#include<iostream>
Using namespace std;
Enum week {Monday=3, Tuesday, Wednesday, Thursday, Friday, Saturday=9,
Sunday};
int main()
{
week day,a;
day=Friday;
cout<<"Day:"<<day+1<<endl;
a=Saturday;
cout<<"Day:"<<a+1<<endl; //10
return0;
}
Array
• C++ Arrays
• Like other programming languages, array in C++ is a group of similar types
of elements that have contiguous memory location.
• In C++ std::array is a container that encapsulates fixed size arrays. In C++,
array index starts from 0. We can store only fixed set of elements in C++
array.
Array
• C++ Single Dimensional Array
• Let's see a simple example of C++ array, where we are going to create,
initialize and traverse array.
#include<iostream>
Using namespace std;
Int main()
{
Int a[5]={10,0,20,0,30};//creating and initializing array
//traversing array
for(int i=0;i<5;i++)
{
cout<<a[i]<<"\n"; //a[0], a[1],a[2],a[3]
}
}
String
• C++ Strings
• In C++, string is an object of std::string class that represents sequence of
characters. We can perform many operations on strings such as
concatenation, comparison, conversion etc.
•Class :
1) Class is a collection of objects.
2) Class is a user defined data type.
3) Class encapsulate data members and
member functions.
4) Class is an Abstract Data Type.
Class
• Class :
1) Class is a collection of objects.
Class Student
{
int roll_number;
char name[40];
ENCAPSULATION
void avgMarks();
void totalMarks();
}
Class
• Class :
4) Class is an Abstract Data Type (ADT).
•Object :
1) Object is real world entity.
2) Object is an instance of a class.
3) Object is run time entity in an Object
Oriented System.
4) Object is variable of class.
Object
• Object :
1) Object is real world entity.
Function Prototype:-
Functions
Function Prototype:-
Functions
Accessing Function :-
Access functions can read or display data. Another common use
for access functions is to test the truth or falsity of conditions such
functions are often called predicate functions.
Utility Function :-
A utility function is not part of a class's public interface; rather, it
is a private member function that supports the operation of the
class's public member functions. Utility functions are not
intended to be used by clients of a class (but can be used by
friends of a class,
Functions
Accessing Function :-
class IntArray
#include <iostream> {
private:
class Something
{ int m_array[10]; // user can not access
private: this directly any more
int m_value1;
int m_value2; public:
int m_value3; void setValue(int index, int value)
{
public: // If the index is invalid, do nothing
void setValue1(int value) { m_value1 = value; }
if (index < 0 || index >=
int getValue1() { return m_value1; }
}; m_array.size())
return;
int main()
{ m_array[index] = value;
Something something; }
something.setValue1(5); };
std::cout << something.getValue1() << '\n';
}
Functions
Utility Function :-
class SalesPerson
8{
9 public:
10 SalesPerson(); // constructor
11 void getSalesFromUser(); // input sales from keyboard
12 void setSales( int, double ); // set sales for a specific month
13 void printAnnualSales(); //summarize and print sales
14 private:
15 double totalAnnualSales(); // prototype for utility function
16 double sales[ 12 ]; // 12 monthly sales figures
17 }; // end class SalesPerson
18
void SalesPerson::printAnnualSales()
47 {
48 cout << setprecision( 2 ) << fixed
49 << "The total annual sales are: $"
50 << totalAnnualSales() << endl; // call utility function
51 } // end function printAnnualSales
Constructor
A constructor in C++ is a special method that is
automatically called when an object of a class is
created.
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
Car carObj2("Ford", "Mustang", 1969);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
Constructor
Just like functions, constructors can also be defined outside the
class.
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z); // Constructor declaration
};
int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 1999);
// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
Constructor (Copy Constructor)
A copy constructor is a member function that initializes an
object using another object of the same class.
Constructor (Copy Constructor)
Constructor (Copy Constructor)
Constructor (Copy Constructor)
Constructor (Copy Constructor)
Constructor (Copy Constructor)
Constructor (Copy Constructor)
Destructor
• A destructor is a member function that is invoked automatically
when the object goes out of scope or is explicitly destroyed by a call
to delete
#include<iostream>
using namespace std; int main()
{
class GfG GfG obj1;
{ GfG obj2;
public: obj1.i =2;
static int i; obj2.i = 3;
int GfG :: i = 1;
Static Variables (Static Object)
// CPP program to illustrate
// when not using static keyword int main()
#include<iostream> {
using namespace std; int x = 0;
if (x==0)
class GfG {
{ GfG obj;
int i; }
public: cout << "End of main\n";
GfG() }
{
i = 0;
cout << "Inside Constructor\n";
}
~GfG()
{
Output: -
cout << "Inside Destructor\n";
Inside Constructor
}
Inside Destructor
};
End of main
Static Variables (Static Object)
// CPP program to illustrate
// when not using static keyword int main()
#include<iostream> {
using namespace std; int x = 0;
if (x==0)
class GfG {
{ Static GfG obj;
int i; }
public: cout << "End of main\n";
GfG() }
{
i = 0;
cout << "Inside Constructor\n";
}
~GfG()
{
Output: -
cout << "Inside Destructor\n";
Inside Constructor
}
End of main
};
Inside Destructor
Static Variables (Static Function)
• static member functions also does not depend on
object of class.
Even though the prototypes for friend functions appear in the class
definition, friends are not member functions.
public:
double length;
friend void printWidth( Box box );
void setWidth( double wid );
};