SlideShare a Scribd company logo
1 of 39
UNIT-5
Introduction to C++ programming:
Object Oriented Programming Concepts, Structured Vs OOP.
Classes and Objects:
Class Definition, Objects, Class Scope and Accessing Members.
Constructors : Default Constructor, Parameterized Constructor,
Constructor initialization list, Copy Constructor and
Destructors.
History of C++
 Extension of C
 Early 1980s: Bjarne Stroustrup (Bell Laboratories)
 Originally named “C with Classes”.
 Provides capabilities for object-oriented programming.
• Objects: Reusable Software Components
– Model items in real world
• Object-Oriented Programs
– Easy to understand, correct and modify
 Hybrid language
• C-like style
• Object-Oriented style
• Both
Object Oriented Programming Concepts
 OOP is a programming style that is focused on objects.
 Object Oriented Programming Concepts
– Classes
– Objects
– Abstraction
– Encapsulation
– Inheritance
– Polymorphism
– Message Passing
 A class is collection of objects of similar type or it is a template.
Ex: fruit mango;
 For example, mango, apple and orange are members of the class fruit.
 Classes are user-defined data types and behave like the built-in types of a
programming language.
 Objects are instances of the type class.
 Objects are the basic run-time entities in an object-oriented system.
class object
Class and Object:
Encapsulation:
 The wrapping up of data and functions into a single unit ( called class)
is known as encapsulation.
 That is the data and method are given in the class definition.
 Data encapsulation is the most striking features of a class.
Abstraction:
 Abstraction refers to the act of representing essential features without
including the background details or explanations.
 The access modifiers in C++ or any OOP language, provides abstraction.
 If a variable is declared as private, then other classes cannot access it.
Polymorphism: Poly – Many Morph – Form
 Polymorphism is the characteristic that enables an entity to co exist in
more than one form.
 Ex: It allows the single method to perform different actions based on the
parameters.
 C++ supports function overloading and operator overloading to
implement polymorphism
Inheritance:
 Inheritance is the process by which objects of one class acquire the
properties of another class. The concept of inheritance provides the
reusability.
 A class can inherit one or more classes. Inherited class is called as parent
class or super class or base class. Class that inherits a parent class is
called as child class or sub class or derived class.
Drawing Object
Multiple-Point Object Two-Point Object
Polygon Curve Rectangle Line
Example for Inheritance
Example for Polymorphism
Shape
Draw()
Circle object
Draw()
Box object
Draw()
Triangle
Draw()
Message passing:
Set of objects that communicate with each other.
1. Create the classes that define objects and their behavior.
2. Creating the objects for that class.
3. Establish the communication among objects.
Example: account. Balance_enquiry(accountno);
Object message information
Dynamic Binding:
When a method is called within a program, it associated with the
program at run time rather than at compile time is called dynamic
binding.
Structured Oriented Programming:
Main program
Function-5
Function-2 Function-3
Function-4
Function-1
Function-6 Function-7 Function-8
Global data Global data
Function-1
Local data
Function-2
Local data
Function-3
Local data
Relationship of Data &
Functions in Structured
Programming
 Program is divided into functions, every function has its own data and global
data
OOP decompose the problem into number of entities called objects and it
contains the data and functions.
Object A Object B
Data
Functions
Data
Functions
Data
Functions
Object C
Communication
Object Oriented Programming
Characteristics of Structure-Oriented Programming
 Emphasis is on doing things.
 Large programs are divided into smaller programs known as functions.
 Most of the functions share global data.
 Data move openly around the system from function to function
 Functions transform data from one form to another
 Employs top-down approach in program design
Characteristics of Object-Oriented Programming
 Emphasis is on data rather than procedure.
 Programs are divided into what are known as objects.
 Functions that operate on the data of an object are tied together in the data
structure.
 Data is hidden and cannot be accessed by external functions.
 Objects may communicate with each other through functions.
 Employs bottom-up approach in program design
 New data and functions can be easily added whenever necessary.
Structured Vs OOP
Top Down approach:
– A Single module will be split into several smaller modules
– General to Specific
Bottom Up approach:
– Lot of small modules will be grouped to form a single large module
– Specific to General
 If the requirements are clear at the first instance we can go for Top Down
approach.
 In circumstances where the requirements may keep on adding, we go for
Bottom Up approach.
Benefits of OOP
Through inheritance, we can eliminate redundant code and extend the use of
existing classes.
The principle of data hiding helps the programmer to build secure programs
that cannot be invaded by code in the parts of the program.
 It is easy to partition the work in a project based on objects.
 Object oriented system easily upgraded from small to large systems.
 Software complexity can be easily managed.
Applications of OOP
Real-time systems.
Object-Oriented Databases.
Neural Networks and Parallel Programming.
Decision Support and Office Automation Systems.
I/O in C++
 Since C++ is a superset of C, all of the C I/O functions such as printf and
scanf which are found in the stdio.h header file, are still valid in C++.
 C++ provides an alternative with the new stream input/output features by
including “iostream.h”.
 Several new I/O objects available when you include the iostream header file.
Two important ones are:
 cin // Used for keyboard input
 cout // Used for screen output
 Both cin and cout can be combined with other member functions for a wide
variety of special I/O capabilities in program applications.
 Since cin and cout are C++ objects, they are somewhat "intelligent":
 They do not require the usual format strings and conversion specifications.
 They do automatically know what data types are involved.
 They do not need the address operator, &.
 They do require the use of the stream extraction (>>) and insertion (<<) operators.
 Example with cin and cout:
// program to find average of two numbers
#include<iostream.h>
void main()
{
float n1,n2,avg;
cout<<”Enter two valuesn”;
cin>>n1>>n2;
avg = (n1+n2)/2;
cout<<”nAverage is “<<avg;
}
Creating Classes:
 The class is basis for the OOP. The class is used to define the nature of
an object, and also it is basic unit for the encapsulation.
 The class combines the data and its associated functions together. It allows
the data to be hidden.
 The keyword class is used to create a class.
Class has two parts:
1. Data members declaration
2. Prototype of member function declarations
Note: Data members can’t be initialized with in the class. They can be
initialized using member functions of that class.
class: item
Data
number
cost
Functions
getdata()
putdata()
(a)
getdata()
putdata()
item
(b)
number
cost
Representation of class
Syntax of declaring a class:
class class-name {
private data and functions;
access-specifier:
data and functions;
access-specifier:
data and functions;
// ...
access-specifier:
data and functions;
}object-list;
 Class should enclose both data declaration and function declaration part between
curly parenthesis and class definition should be ended with semicolon.
 Members (data members and member functions) are grouped under access specifiers,
namely private, public and protected, which define the visibility of members.
 The object-list is optional. If present, it declares objects of the class
Creating objects
 The process of creating objects of a class is called class instantiation.
 Object is instance of the class.
 Once the class is created we can create any number of objects.
class class-name
{
------;
------;
}object-list;
class student
{
------;
------;
}s1,s2,s3;
class_name object-name1,object-name2,object-name3;
student s1, s2, s3;
Syntax1: Example1:
Syntax2:
Example2:
Accessing class members:
 Once an object of a class has been created, then we can access the members
of the class. This is achieved by using the member access operator, dot(.).
Syntax:
Object-Name.DataMember;
Object-Name.Memberfunction(arguments);
Example:
x.getdata(10,20);
x.number;
Here x.number accessing is illegal, if number is private data member of the
class. Private data members are accessed only through the member functions
not directly by objects.
Defining the member functions:
We can define the function in two ways:
1. Outside the class definition 2. Inside the class definition
Outside the class definition: Use the Scope Resolution Operator :: to define a
member function outside of the class.
:: Scope Resolution Operator tells the compiler that to which class the specified
function belongs to.
Syntax:
Return_type class-name:: function-name(argmlist)
{
----
----
}
Inside the class definition: We can directly define a member function inside of the
class without using Scope Resolution Operator.
Structure of C++ program:
Include files
Class declaration
Member functions definitions
Main function program
#include<iostream.h>
#include<string.h>
class student
{
private:
int rollno;
char name[30];
public:
void setdata(int rn, char *n)
{
rollno=rn;
strcpy(name, n);
}
void putdata()
{
cout<<"RollNumber="<<rollno<<" ";
cout<<"Name="<<name<<"n";
}
};
void main()
{
student s1,s2;
s1.setdata(2361,"RAMU");
s2.setdata(2362,"VENKAT");
s1.putdata();
s2.putdata();
}
RollNumber=2361 Name=RAMU
RollNumber=2362Name=VENKAT
//Program for implementing member functions inside of a class
#include<iostream.h> #include<string.h>
class student
{
private:
int rollno;
char name[30];
public:
void setdata(int rn, char *n)
void putdata()
};
void student :: setdata(int rn, char *n)
{
rollno=rn;
strcpy(name, n);
}
void student :: putdata()
{
cout<<“RollNumber=“<<rollno<<“ ”;
cout<<“Name=“<<name<<“n”;
}
void main()
{
student s1,s2;
s1.setdata(2361,"RAMU");
s2.setdata(2362,"VENKAT");
s1.putdata();
s2.putdata();
}
RollNumber=2361 Name=RAMU
RollNumber=2362 Name=VENKAT
//Program for implementing member functions outside of a class using
Scope Resolution Operator.
Class Scope:
 It tells in which parts of the program (or in which functions), the class
declaration can be used.
 Class scope may be local or global.
Local:
 If class is declared inside the function, then objects can be created inside
that function only.
Global:
 If class is declared outside the functions, then object can be created in any
function.
#include<iostream.h>
#include<conio.h>
void print() {
class point
{
int a;
public:
void getdata() {
cout<<"Enter an integer number: ";
cin>>a;
}
void display() {
cout<<"Entered number is: "<<a;
}
};
point p;
p.getdata();
p.display();
}
void main()
{
clrscr();
print();
getch();
}
//Example program for class inside a member
function
#include<iostream.h>
#include<conio.h>
class point
{
int a;
public:
void getdata()
{
cout<<"nEnter value of a :";
cin>>a;
}
void display( )
{
cout<<"nValue of a is: "<<a;
}
};
void print() {
point p;
p.getdata();
p.display();
}
void main() {
clrscr();
print();
point m;
m.getdata();
m.display();
getch();
}
//Example program for class outside a member function
The following are the access specifiers:
1. Private 2. Public 3. Protected
Default access specifier is private.
Access Specifiers
Private:
 Private members of a class have strict access control.
 Only the member functions of the same class can access these members.
 They prevents the accidental modifications from the outside world.
Example:
class person
{
private:
int age;
int getage();
};
 A private member functions is only called by the member function of the
same class . Even an object cannot invoke a private function using the dot
operator.
void main(){
person p1;
p1.age=5; // error
p1.getage(); // error
}
int person::getage(){
age=22; //correct
}
Public:
 All members declared with public can have access to outside of the class
without any restrictions.
Example:
class person
{
public:
int age;
int getage();
};
int person :: getage(){
age=10; //correct
}
void main(){
person p1;
p1.age=20;//correct
cout<<p1.age;
p1.getage(); // correct
}
Protected:
 Similar to the private members and used in inheritance.
 The members which are under protected, can have access to the members
of its derived class.
Example: class A
{
private:
//members of A
protected:
//members of A
public:
//members of A
};
class B: public A //Here class B can have access on
{ protected data of its parent class as
//members of B well as public data.
};
Constructors:
 A constructor is a special member function whose task is to initialize the
objects of its class.
 It is special because its name is the same as the class name.
 The constructor is invoked whenever an object of its associated class is
created.
Syntax:
class Test
{
int m, n;
public:
Test();
//constructor declaration
----------
----------
};
Test :: Test() //constructor definition
{
m=0;
n=0;
}
void main(){
Test t; //constructor is invoked
//after creation of object t
}
Characteristics of Constructors:
 Constructors should be declared in the public section.
 They are invoked automatically when the objects are created.
 They do not have return types, not even void and therefore, they cannot
return values.
 They cannot be inherited, though a derived class can call the base class
constructor.
 Like other C++ functions, they can have default arguments.
 We cannot refer to their address.
 Constructors / Destructors cannot be const.
 They make ‘implicit calls’ to the operators new and delete when memory
allocation/de-allocation is required.
Types of Constructors:
1) Default Constructor 2) Parameterized Constructor
3) Copy Constructor
Default constructor:
 Constructor without parameters is called default constructor.
#include<iostream.h>
class point {
int a;
public:
point( ) { //Default constructor
a=1000;
}
void display( ){
cout<<”a value is “<<a;
}
};
void main( ) {
point p; //constructor is invoked after creation of object p
p.display();
}
Parameterized Constructor:
 Constructor which takes parameters is called Parameterized Constructor.
Example:
#include<iostream.h>
class A
{
int m, n;
public:
A (int x, int y); // parameterized constructor
};
A :: A (int x, int y)
{
m=x; n=y;
cout<<m<<n;
}
void main(){
A obj(10,20); or A(10,20);
}
Copy constructor:
 Copy Constructor is used to create an object that is a copy of an existing
object.
 Copy constructor is a member function which is used to initialize an object
from another object.
 By default, the compiler generates a copy constructor for each class.
Syntax:
class-name (class-name &variable)
{
-----;
-----;
};
You can call or invoke copy constructor in the following way:
class obj2(obj1);
or
class obj2 = obj1;
#include<iostream.h>
class point {
int a;
public:
point( ) { //Default Constructor
a=1000;
}
point(int x) { //Parameterized Constructor
a = x;
}
point(point &p) { //Copy Constructor
a = p.a;
}
void display( )
{
cout<<”a value is “<<a;
}
};
void main( )
{
point p1;
point p2(500);
point p3(p1);
p1.display();
p2.display();
p3.display();
}
//Example for Copy Constructor
Destructors
 Destructors are used to de-allocate memory for a class object and its class
members when the object is destroyed.
 A destructor is called for a class object when that object passes out of scope or
is explicitly deleted.
 A destructor is a member function with the same name as its class prefixed
by a ~ (tilde).
 A destructor takes no arguments and has no return type.
 Destructors cannot be declared const or static.
 A destructor can be declared virtual or pure virtual.
 If no user-defined destructor exists for a class, the compiler implicitly declares
a destructor.
Syntax:
class X
{
public: // Constructor for class X
X();
~X(); // Destructor for class X
};
include<iostream.h>
class des {
int a;
public:
des( ) { //Default constructor
a=1000;
}
~des( ) { //Destructor
cout<<”Destructor called”;
}
void display( ) {
cout<<endl<<”a value is “<<a;
}
};
void main( ) {
des p; //Constructor is invoked after creation of object p
p.display();
}
//Example program for Default Constructor

More Related Content

What's hot

What's hot (20)

Constructors and Destructors
Constructors and DestructorsConstructors and Destructors
Constructors and Destructors
 
C programming - String
C programming - StringC programming - String
C programming - String
 
Presentation on pointer.
Presentation on pointer.Presentation on pointer.
Presentation on pointer.
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
C Programming Unit-4
C Programming Unit-4C Programming Unit-4
C Programming Unit-4
 
AOS Lab 2: Hello, xv6!
AOS Lab 2: Hello, xv6!AOS Lab 2: Hello, xv6!
AOS Lab 2: Hello, xv6!
 
11 Unit 1 Chapter 03 Data Handling
11   Unit 1 Chapter 03 Data Handling11   Unit 1 Chapter 03 Data Handling
11 Unit 1 Chapter 03 Data Handling
 
Abstract class in c++
Abstract class in c++Abstract class in c++
Abstract class in c++
 
Exception handling in python
Exception handling in pythonException handling in python
Exception handling in python
 
Programming with Python
Programming with PythonProgramming with Python
Programming with Python
 
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCEFUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
FUNCTIONS IN PYTHON, CLASS 12 COMPUTER SCIENCE
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Data modeling using the entity relationship model
Data modeling using the entity relationship modelData modeling using the entity relationship model
Data modeling using the entity relationship model
 
Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)Fundamental of C Programming (Data Types)
Fundamental of C Programming (Data Types)
 
Array and string
Array and stringArray and string
Array and string
 
C Programming Unit-5
C Programming Unit-5C Programming Unit-5
C Programming Unit-5
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Java(Polymorphism)
Java(Polymorphism)Java(Polymorphism)
Java(Polymorphism)
 
358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7358 33 powerpoint-slides_7-structures_chapter-7
358 33 powerpoint-slides_7-structures_chapter-7
 
Programming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture NotesProgramming For Problem Solving Lecture Notes
Programming For Problem Solving Lecture Notes
 

Similar to Unit 5.ppt

Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Languagedheva B
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++Amresh Raj
 
C++Day-1 Introduction.ppt
C++Day-1 Introduction.pptC++Day-1 Introduction.ppt
C++Day-1 Introduction.pptcitizen15
 
1 intro
1 intro1 intro
1 introabha48
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]Rome468
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesFellowBuddy.com
 
c++session 1.pptx
c++session 1.pptxc++session 1.pptx
c++session 1.pptxPadmaN24
 
Cs6301 programming and datastactures
Cs6301 programming and datastacturesCs6301 programming and datastactures
Cs6301 programming and datastacturesK.s. Ramesh
 
Object oriented programming C++
Object oriented programming C++Object oriented programming C++
Object oriented programming C++AkshtaSuryawanshi
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introductionsandeep54552
 
Ch.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objectsCh.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objectsITNet
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPTAjay Chimmani
 
CPP_,module2_1.pptx
CPP_,module2_1.pptxCPP_,module2_1.pptx
CPP_,module2_1.pptxAbhilashTom4
 

Similar to Unit 5.ppt (20)

Object Oriented Language
Object Oriented LanguageObject Oriented Language
Object Oriented Language
 
Oops
OopsOops
Oops
 
Interoduction to c++
Interoduction to c++Interoduction to c++
Interoduction to c++
 
My c++
My c++My c++
My c++
 
C++Day-1 Introduction.ppt
C++Day-1 Introduction.pptC++Day-1 Introduction.ppt
C++Day-1 Introduction.ppt
 
C++
C++C++
C++
 
1 intro
1 intro1 intro
1 intro
 
C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]C++ [ principles of object oriented programming ]
C++ [ principles of object oriented programming ]
 
C++ & VISUAL C++
C++ & VISUAL C++ C++ & VISUAL C++
C++ & VISUAL C++
 
Object Oriented Programming Lecture Notes
Object Oriented Programming Lecture NotesObject Oriented Programming Lecture Notes
Object Oriented Programming Lecture Notes
 
c++session 1.pptx
c++session 1.pptxc++session 1.pptx
c++session 1.pptx
 
Topic 1 PBO
Topic 1 PBOTopic 1 PBO
Topic 1 PBO
 
Cs6301 programming and datastactures
Cs6301 programming and datastacturesCs6301 programming and datastactures
Cs6301 programming and datastactures
 
chapter - 1.ppt
chapter - 1.pptchapter - 1.ppt
chapter - 1.ppt
 
Object oriented programming C++
Object oriented programming C++Object oriented programming C++
Object oriented programming C++
 
The smartpath information systems c plus plus
The smartpath information systems  c plus plusThe smartpath information systems  c plus plus
The smartpath information systems c plus plus
 
C++ programming introduction
C++ programming introductionC++ programming introduction
C++ programming introduction
 
Ch.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objectsCh.1 oop introduction, classes and objects
Ch.1 oop introduction, classes and objects
 
4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT4 pillars of OOPS CONCEPT
4 pillars of OOPS CONCEPT
 
CPP_,module2_1.pptx
CPP_,module2_1.pptxCPP_,module2_1.pptx
CPP_,module2_1.pptx
 

Recently uploaded

8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...josephjonse
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdfAldoGarca30
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxhublikarsn
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesRashidFaridChishti
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementDr. Deepak Mudgal
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARKOUSTAV SARKAR
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxNANDHAKUMARA10
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessorAshwiniTodkar4
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdfKamal Acharya
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptAfnanAhmad53
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdfKamal Acharya
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxMustafa Ahmed
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfsumitt6_25730773
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxpritamlangde
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...ssuserdfc773
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 

Recently uploaded (20)

8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...8th International Conference on Soft Computing, Mathematics and Control (SMC ...
8th International Conference on Soft Computing, Mathematics and Control (SMC ...
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
1_Introduction + EAM Vocabulary + how to navigate in EAM.pdf
 
Introduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptxIntroduction to Robotics in Mechanical Engineering.pptx
Introduction to Robotics in Mechanical Engineering.pptx
 
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using PipesLinux Systems Programming: Inter Process Communication (IPC) using Pipes
Linux Systems Programming: Inter Process Communication (IPC) using Pipes
 
Ground Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth ReinforcementGround Improvement Technique: Earth Reinforcement
Ground Improvement Technique: Earth Reinforcement
 
Signal Processing and Linear System Analysis
Signal Processing and Linear System AnalysisSignal Processing and Linear System Analysis
Signal Processing and Linear System Analysis
 
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKARHAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
HAND TOOLS USED AT ELECTRONICS WORK PRESENTED BY KOUSTAV SARKAR
 
Electromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptxElectromagnetic relays used for power system .pptx
Electromagnetic relays used for power system .pptx
 
8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor8086 Microprocessor Architecture: 16-bit microprocessor
8086 Microprocessor Architecture: 16-bit microprocessor
 
Hospital management system project report.pdf
Hospital management system project report.pdfHospital management system project report.pdf
Hospital management system project report.pdf
 
fitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .pptfitting shop and tools used in fitting shop .ppt
fitting shop and tools used in fitting shop .ppt
 
Post office management system project ..pdf
Post office management system project ..pdfPost office management system project ..pdf
Post office management system project ..pdf
 
Augmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptxAugmented Reality (AR) with Augin Software.pptx
Augmented Reality (AR) with Augin Software.pptx
 
Introduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdfIntroduction to Data Visualization,Matplotlib.pdf
Introduction to Data Visualization,Matplotlib.pdf
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Digital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptxDigital Communication Essentials: DPCM, DM, and ADM .pptx
Digital Communication Essentials: DPCM, DM, and ADM .pptx
 
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
Convergence of Robotics and Gen AI offers excellent opportunities for Entrepr...
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 

Unit 5.ppt

  • 2. Introduction to C++ programming: Object Oriented Programming Concepts, Structured Vs OOP. Classes and Objects: Class Definition, Objects, Class Scope and Accessing Members. Constructors : Default Constructor, Parameterized Constructor, Constructor initialization list, Copy Constructor and Destructors.
  • 3. History of C++  Extension of C  Early 1980s: Bjarne Stroustrup (Bell Laboratories)  Originally named “C with Classes”.  Provides capabilities for object-oriented programming. • Objects: Reusable Software Components – Model items in real world • Object-Oriented Programs – Easy to understand, correct and modify  Hybrid language • C-like style • Object-Oriented style • Both
  • 4. Object Oriented Programming Concepts  OOP is a programming style that is focused on objects.  Object Oriented Programming Concepts – Classes – Objects – Abstraction – Encapsulation – Inheritance – Polymorphism – Message Passing
  • 5.  A class is collection of objects of similar type or it is a template. Ex: fruit mango;  For example, mango, apple and orange are members of the class fruit.  Classes are user-defined data types and behave like the built-in types of a programming language.  Objects are instances of the type class.  Objects are the basic run-time entities in an object-oriented system. class object Class and Object:
  • 6. Encapsulation:  The wrapping up of data and functions into a single unit ( called class) is known as encapsulation.  That is the data and method are given in the class definition.  Data encapsulation is the most striking features of a class. Abstraction:  Abstraction refers to the act of representing essential features without including the background details or explanations.  The access modifiers in C++ or any OOP language, provides abstraction.  If a variable is declared as private, then other classes cannot access it.
  • 7. Polymorphism: Poly – Many Morph – Form  Polymorphism is the characteristic that enables an entity to co exist in more than one form.  Ex: It allows the single method to perform different actions based on the parameters.  C++ supports function overloading and operator overloading to implement polymorphism Inheritance:  Inheritance is the process by which objects of one class acquire the properties of another class. The concept of inheritance provides the reusability.  A class can inherit one or more classes. Inherited class is called as parent class or super class or base class. Class that inherits a parent class is called as child class or sub class or derived class.
  • 8. Drawing Object Multiple-Point Object Two-Point Object Polygon Curve Rectangle Line Example for Inheritance Example for Polymorphism Shape Draw() Circle object Draw() Box object Draw() Triangle Draw()
  • 9. Message passing: Set of objects that communicate with each other. 1. Create the classes that define objects and their behavior. 2. Creating the objects for that class. 3. Establish the communication among objects. Example: account. Balance_enquiry(accountno); Object message information Dynamic Binding: When a method is called within a program, it associated with the program at run time rather than at compile time is called dynamic binding.
  • 10. Structured Oriented Programming: Main program Function-5 Function-2 Function-3 Function-4 Function-1 Function-6 Function-7 Function-8 Global data Global data Function-1 Local data Function-2 Local data Function-3 Local data Relationship of Data & Functions in Structured Programming  Program is divided into functions, every function has its own data and global data
  • 11. OOP decompose the problem into number of entities called objects and it contains the data and functions. Object A Object B Data Functions Data Functions Data Functions Object C Communication Object Oriented Programming
  • 12. Characteristics of Structure-Oriented Programming  Emphasis is on doing things.  Large programs are divided into smaller programs known as functions.  Most of the functions share global data.  Data move openly around the system from function to function  Functions transform data from one form to another  Employs top-down approach in program design Characteristics of Object-Oriented Programming  Emphasis is on data rather than procedure.  Programs are divided into what are known as objects.  Functions that operate on the data of an object are tied together in the data structure.  Data is hidden and cannot be accessed by external functions.  Objects may communicate with each other through functions.  Employs bottom-up approach in program design  New data and functions can be easily added whenever necessary. Structured Vs OOP
  • 13. Top Down approach: – A Single module will be split into several smaller modules – General to Specific Bottom Up approach: – Lot of small modules will be grouped to form a single large module – Specific to General  If the requirements are clear at the first instance we can go for Top Down approach.  In circumstances where the requirements may keep on adding, we go for Bottom Up approach.
  • 14. Benefits of OOP Through inheritance, we can eliminate redundant code and extend the use of existing classes. The principle of data hiding helps the programmer to build secure programs that cannot be invaded by code in the parts of the program.  It is easy to partition the work in a project based on objects.  Object oriented system easily upgraded from small to large systems.  Software complexity can be easily managed. Applications of OOP Real-time systems. Object-Oriented Databases. Neural Networks and Parallel Programming. Decision Support and Office Automation Systems.
  • 15. I/O in C++  Since C++ is a superset of C, all of the C I/O functions such as printf and scanf which are found in the stdio.h header file, are still valid in C++.  C++ provides an alternative with the new stream input/output features by including “iostream.h”.  Several new I/O objects available when you include the iostream header file. Two important ones are:  cin // Used for keyboard input  cout // Used for screen output  Both cin and cout can be combined with other member functions for a wide variety of special I/O capabilities in program applications.
  • 16.  Since cin and cout are C++ objects, they are somewhat "intelligent":  They do not require the usual format strings and conversion specifications.  They do automatically know what data types are involved.  They do not need the address operator, &.  They do require the use of the stream extraction (>>) and insertion (<<) operators.  Example with cin and cout: // program to find average of two numbers #include<iostream.h> void main() { float n1,n2,avg; cout<<”Enter two valuesn”; cin>>n1>>n2; avg = (n1+n2)/2; cout<<”nAverage is “<<avg; }
  • 17. Creating Classes:  The class is basis for the OOP. The class is used to define the nature of an object, and also it is basic unit for the encapsulation.  The class combines the data and its associated functions together. It allows the data to be hidden.  The keyword class is used to create a class. Class has two parts: 1. Data members declaration 2. Prototype of member function declarations Note: Data members can’t be initialized with in the class. They can be initialized using member functions of that class.
  • 19. Syntax of declaring a class: class class-name { private data and functions; access-specifier: data and functions; access-specifier: data and functions; // ... access-specifier: data and functions; }object-list;  Class should enclose both data declaration and function declaration part between curly parenthesis and class definition should be ended with semicolon.  Members (data members and member functions) are grouped under access specifiers, namely private, public and protected, which define the visibility of members.  The object-list is optional. If present, it declares objects of the class
  • 20. Creating objects  The process of creating objects of a class is called class instantiation.  Object is instance of the class.  Once the class is created we can create any number of objects. class class-name { ------; ------; }object-list; class student { ------; ------; }s1,s2,s3; class_name object-name1,object-name2,object-name3; student s1, s2, s3; Syntax1: Example1: Syntax2: Example2:
  • 21. Accessing class members:  Once an object of a class has been created, then we can access the members of the class. This is achieved by using the member access operator, dot(.). Syntax: Object-Name.DataMember; Object-Name.Memberfunction(arguments); Example: x.getdata(10,20); x.number; Here x.number accessing is illegal, if number is private data member of the class. Private data members are accessed only through the member functions not directly by objects.
  • 22. Defining the member functions: We can define the function in two ways: 1. Outside the class definition 2. Inside the class definition Outside the class definition: Use the Scope Resolution Operator :: to define a member function outside of the class. :: Scope Resolution Operator tells the compiler that to which class the specified function belongs to. Syntax: Return_type class-name:: function-name(argmlist) { ---- ---- } Inside the class definition: We can directly define a member function inside of the class without using Scope Resolution Operator.
  • 23. Structure of C++ program: Include files Class declaration Member functions definitions Main function program
  • 24. #include<iostream.h> #include<string.h> class student { private: int rollno; char name[30]; public: void setdata(int rn, char *n) { rollno=rn; strcpy(name, n); } void putdata() { cout<<"RollNumber="<<rollno<<" "; cout<<"Name="<<name<<"n"; } }; void main() { student s1,s2; s1.setdata(2361,"RAMU"); s2.setdata(2362,"VENKAT"); s1.putdata(); s2.putdata(); } RollNumber=2361 Name=RAMU RollNumber=2362Name=VENKAT //Program for implementing member functions inside of a class
  • 25. #include<iostream.h> #include<string.h> class student { private: int rollno; char name[30]; public: void setdata(int rn, char *n) void putdata() }; void student :: setdata(int rn, char *n) { rollno=rn; strcpy(name, n); } void student :: putdata() { cout<<“RollNumber=“<<rollno<<“ ”; cout<<“Name=“<<name<<“n”; } void main() { student s1,s2; s1.setdata(2361,"RAMU"); s2.setdata(2362,"VENKAT"); s1.putdata(); s2.putdata(); } RollNumber=2361 Name=RAMU RollNumber=2362 Name=VENKAT //Program for implementing member functions outside of a class using Scope Resolution Operator.
  • 26. Class Scope:  It tells in which parts of the program (or in which functions), the class declaration can be used.  Class scope may be local or global. Local:  If class is declared inside the function, then objects can be created inside that function only. Global:  If class is declared outside the functions, then object can be created in any function.
  • 27. #include<iostream.h> #include<conio.h> void print() { class point { int a; public: void getdata() { cout<<"Enter an integer number: "; cin>>a; } void display() { cout<<"Entered number is: "<<a; } }; point p; p.getdata(); p.display(); } void main() { clrscr(); print(); getch(); } //Example program for class inside a member function
  • 28. #include<iostream.h> #include<conio.h> class point { int a; public: void getdata() { cout<<"nEnter value of a :"; cin>>a; } void display( ) { cout<<"nValue of a is: "<<a; } }; void print() { point p; p.getdata(); p.display(); } void main() { clrscr(); print(); point m; m.getdata(); m.display(); getch(); } //Example program for class outside a member function
  • 29. The following are the access specifiers: 1. Private 2. Public 3. Protected Default access specifier is private. Access Specifiers Private:  Private members of a class have strict access control.  Only the member functions of the same class can access these members.  They prevents the accidental modifications from the outside world. Example: class person { private: int age; int getage(); };  A private member functions is only called by the member function of the same class . Even an object cannot invoke a private function using the dot operator. void main(){ person p1; p1.age=5; // error p1.getage(); // error } int person::getage(){ age=22; //correct }
  • 30. Public:  All members declared with public can have access to outside of the class without any restrictions. Example: class person { public: int age; int getage(); }; int person :: getage(){ age=10; //correct } void main(){ person p1; p1.age=20;//correct cout<<p1.age; p1.getage(); // correct }
  • 31. Protected:  Similar to the private members and used in inheritance.  The members which are under protected, can have access to the members of its derived class. Example: class A { private: //members of A protected: //members of A public: //members of A }; class B: public A //Here class B can have access on { protected data of its parent class as //members of B well as public data. };
  • 32. Constructors:  A constructor is a special member function whose task is to initialize the objects of its class.  It is special because its name is the same as the class name.  The constructor is invoked whenever an object of its associated class is created. Syntax: class Test { int m, n; public: Test(); //constructor declaration ---------- ---------- }; Test :: Test() //constructor definition { m=0; n=0; } void main(){ Test t; //constructor is invoked //after creation of object t }
  • 33. Characteristics of Constructors:  Constructors should be declared in the public section.  They are invoked automatically when the objects are created.  They do not have return types, not even void and therefore, they cannot return values.  They cannot be inherited, though a derived class can call the base class constructor.  Like other C++ functions, they can have default arguments.  We cannot refer to their address.  Constructors / Destructors cannot be const.  They make ‘implicit calls’ to the operators new and delete when memory allocation/de-allocation is required.
  • 34. Types of Constructors: 1) Default Constructor 2) Parameterized Constructor 3) Copy Constructor Default constructor:  Constructor without parameters is called default constructor. #include<iostream.h> class point { int a; public: point( ) { //Default constructor a=1000; } void display( ){ cout<<”a value is “<<a; } }; void main( ) { point p; //constructor is invoked after creation of object p p.display(); }
  • 35. Parameterized Constructor:  Constructor which takes parameters is called Parameterized Constructor. Example: #include<iostream.h> class A { int m, n; public: A (int x, int y); // parameterized constructor }; A :: A (int x, int y) { m=x; n=y; cout<<m<<n; } void main(){ A obj(10,20); or A(10,20); }
  • 36. Copy constructor:  Copy Constructor is used to create an object that is a copy of an existing object.  Copy constructor is a member function which is used to initialize an object from another object.  By default, the compiler generates a copy constructor for each class. Syntax: class-name (class-name &variable) { -----; -----; }; You can call or invoke copy constructor in the following way: class obj2(obj1); or class obj2 = obj1;
  • 37. #include<iostream.h> class point { int a; public: point( ) { //Default Constructor a=1000; } point(int x) { //Parameterized Constructor a = x; } point(point &p) { //Copy Constructor a = p.a; } void display( ) { cout<<”a value is “<<a; } }; void main( ) { point p1; point p2(500); point p3(p1); p1.display(); p2.display(); p3.display(); } //Example for Copy Constructor
  • 38. Destructors  Destructors are used to de-allocate memory for a class object and its class members when the object is destroyed.  A destructor is called for a class object when that object passes out of scope or is explicitly deleted.  A destructor is a member function with the same name as its class prefixed by a ~ (tilde).  A destructor takes no arguments and has no return type.  Destructors cannot be declared const or static.  A destructor can be declared virtual or pure virtual.  If no user-defined destructor exists for a class, the compiler implicitly declares a destructor. Syntax: class X { public: // Constructor for class X X(); ~X(); // Destructor for class X };
  • 39. include<iostream.h> class des { int a; public: des( ) { //Default constructor a=1000; } ~des( ) { //Destructor cout<<”Destructor called”; } void display( ) { cout<<endl<<”a value is “<<a; } }; void main( ) { des p; //Constructor is invoked after creation of object p p.display(); } //Example program for Default Constructor