Oop Chapter 1
Oop Chapter 1
(CPE2402L01)
Unit 1
Fundamentals of Object Oriented
Programming
Mrs. Sonal Mohite
(Computer Department , DYPCOE)
Contents:
• Difference between procedure oriented(POP) and object oriented
programming (OOP)
• Basic concepts of object oriented programming in C++
• C++ programming-classes, objects, array of objects, member functions,
access specifiers, friend function, friend class, static variables, static
function, inline functions, this pointer, namespaces
• Constructor- Types: Default, parameterized, copy, Destructors
Introduction
• C++ is an object oriented programming language.
• C++ is a general purpose, case-sensitive, free-form
programming language that supports object-oriented,
procedural and generic programming.
• C++ is a middle-level language, as it encapsulates
both high and low level language features.
• It is developed by Bjarne Stroustrup at AT&T Bell
Laboratories in Murry Hill, New Jersey, USA in
the early 1980's.
• C++ is an extension of C with major addition of the Class
feature.
• Class was a major addition to the original C language.
Object functions are linked through message Parts of program are linked through parameter
passing. passing.
Adding new data and functions is easy Expanding new data and functions is not easy.
use for solving big problems. Not suitable for solving big problems.
2) Data is less secured in C. In C++, you can use modifiers for class
members to make it inaccessible for
outside users.
7) In C, scanf() and printf() are mainly C++ mainly uses stream cin and cout to
used for input/output. perform input and output operations.
8) Operator overloading is not possible in Operator overloading is possible in
C. C++.
10) C does not provide the feature of C++ supports the feature of
namespace. namespace.
void main()
{
person p1, p2; //p1 is a object
}
Class
• Collection of objects is called class.
• Class is a blueprint of data and functions or methods.
• Class does not take any memory space.
• Class is a user defined data type like structures and unions in C.
• By default class variables are private.
• A class contains members like data members and member
functions.
• Data members are variables of the class.
• Member functions are the methods that are used to manipulate
data members.
• Data members define the properties of the class whereas the
member functions define the behaviour of the class.
• A class can have multiple objects which have properties and
behaviour that in common for all of them.
syntax for class:-
class class_name {
data_type data_member_name;
return_type method_name(parameters);
}
*********************************** OR *****************************
class class_name
{
private:
//data members and member functions declarations
public:
//data members and member functions declarations
protected:
//data members and member functions declarations
};
class student{
int rollno;
char name[20];
public:
void get();
void put();
};
void main(){
student s;
s.get();
}
Data Abstraction
• Data abstraction refers to, providing only
essential information to the outside world and
hiding their background details, i.e., to represent
the needed information in program without
presenting the details.
• Real-time system
• Simulation and modeling
• Object-oriented data bases
• Hypertext, Hypermedia, and expertext
• AI and expert systems
• Neural networks and parallel programming
• Decision support and office automation systems
• CIM/CAM/CAD systems
• Application Software Development
Structure of C++ program
The structure of C++ program is divided into
four different sections:
(1) Header File Section
(2) Class Declaration section
(3) Member Function definition section
(4) Main function section
(1) Header File Section:
For example:
#include<iostream.h>
#include<conio.h>
clrscr();
getch();
---Header file contains declaration and definition
of various built in functions as well as object. In
order to use this built in functions or object we
need to include particular header file in our
program.
(2) Class Declaration Section:
}
Scope resolution operator in C++
In C++, scope resolution operator is :: . It is used
for following purposes.
• Let's see an example to create object of student class using s1 as the reference variable.
• In this example, Student is the type and s1 is the reference variable that refers to the
instance of Student class.
Classes and Objects
Class Definition:
• Syntax:
• string cars[4];
• string cars[4] = {"Volvo", "BMW", "Ford", "Mazda"};
student stud[20];
Mememory allocation for array
of a object
Prg.1—WAP to declare a class 'student' having data members
rollno and name. Accept and display this data for three
objects.
#include<iostream.h>
#include<conio.h>
class student{
int rollno;
char name[30];
public:
void get();
void put();
};
void student::get(){
cout<<”Enter rollno and name”;
cin>>rollno>>name;
}
void student::put(){
cout<<”Roll Number and name:”<<rollno<<name;
}
void main(){
student s[3];
for(int i=0;i<3;i++)
s[i].get();
for(int i=0;i<3;i++)
s[i].put();
getch();
}
Static data members
Following are the characteristics of static member
● It is initialized to zero when the first object of its class is
created. No other initialization is permitted
● Only one copy of that member is created for the entire
class and is shared by all the object of that class, no
matter how many objects are created.
● It is visible only within class, but its lifetime is the entire
program.
● Static variables are used to maintain values common to
the entire class.
●Each static variable must be defined outside the class
definition.
●Static data members are stored separately rather than as
a part of an object.
●Since they are associated with the class itself rather than
with any class object, they are also known as class variable.
<class-name>(list-of-parameters)
{
//constructor definition
}
b. Syntax for defining the constructor outside the class
<class-name>: :<class-name>(list-of-parameters)
{
//constructor definition
}
// Example: defining the constructor within the class
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student()
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}
void display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
};
int main()
{
student s; //constructor gets called automatically when we create the
object of the class
s.display();
return 0;
}
// Example: defining the constructor outside the class
#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student();
void display();
};
student::student()
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}
void student::display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
int main()
{ student s;
s.display();
return 0;
}
Characteristics of constructor
• The name of the constructor is same as its
class name.
• Constructors are mostly declared in the
public section of the class though it 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.
• Constructors can be overloaded.
• Constructor can not be declared virtual.
Types of constructor
#include <iostream>
using namespace std;
class construct {
public:
int a, b;
// Default Constructor
construct()
{
a = 10;
b = 20;
}
};
int main()
{
// Default constructor called automatically
// when the object is created
construct c;
cout << "a: " << c.a << endl << "b: " << c.b;
return 1;
}
Output a: 10
b: 20
2. Parameterized Constructors:
• It is 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 Point {
private:
int x, y;
public:
// Parameterized Constructor
Point(int x1, int y1)
{
x = x1;
y = y1;
}
int getX() { return x; }
int getY() { return y; }
};
int main()
{
// Constructor called
Point p1(10, 15);
return 0;
}
Output
p1.x = 10, p1.y = 15
Uses of Parameterized constructor:
• It is used to initialize the various data elements of
different objects with different values when they are
created.
• It is used to overload constructors.
Can we have more than one constructor in a
class?
Yes, It is called Constructor Overloading.