Escolar Documentos
Profissional Documentos
Cultura Documentos
Objectives
Module 11
Introduction
To
Object Oriented Programming
Slide 1 of 66
Introduction to OOP
Slide 2 of 66
Introduction to OOP
Question
Main function
code?
Function 1
Function 2
Function 4
1. Variables
Function 5
2. Functions
Function 6
Function 3
Slide 3 of 66
Introduction to OOP
Function 7
Function 8
Slide 4 of 66
Introduction to OOP
1. Introduction to OOP
known as a Class.
Member data
Member functions
is by using an Object.
An Object is an instance of a Class.
foad - Curtin, Sarawak
Slide 5 of 66
Introduction to OOP
Slide 6 of 66
Introduction to OOP
Slide 7 of 66
Introduction to OOP
Slide 8 of 66
Introduction to OOP
2. Acsess Specifiers
Check
Module-11-IntroductionToOOP-ClassExamples.pdf file
the class.
for OOP coding Examples.
Public: Contents of the class can be accessed from
Slide 9 of 66
1#include<iostream.h>
2class Add
//This is how you declare the class.
3{
4
private:
5
int num1,num2,num3
//Member data.
6
public:
7
void input(int var1, int var2) //Member function.
8
{
9
cout<<This Function gives values to member data<<endl;
10
num1=var1;
11
num2=var2;
12
}
13
void sum()
//Member function.
14
{
15
cout<<This Function finds the sum of two numbers<<endl;
16
num3=num1+num2;
17
}
18
void display()
//Member function.
19
{
20
cout<<The sum of the two numbers is <<num3<<endl;
21
}
22};
23void main()
24{
25 Add A1;
26 int iX, iY;
27 cout<<Input two numbers<<endl;
28 cin>>iX;
29 cin>>iY;
30 A1.input(iX,iY);
31 A1.sum();
32 A1.display();
33}
Introduction to OOP
Slide 11 of 66
Slide 10 of 66
Introduction to OOP
Slide 12 of 66
Introduction to OOP
Slide 13 of 66
Introduction to OOP
class IsEveryOneHere
Programming in C++ - Module 11
{
Isolate the error in
private:
this code: ????
int iNum;
private:
void TotalStudents()
{
iNum = 164;
cout << "Total Students Are: " << iNum << endl;
}
};
int main()
{
IsEveryOneHere InYourDreams;
InYourDreams.TotalStudents();
system("Pause");
return 0;
}
foad - Curtin, Sarawak
Slide 15 of 66
Introduction to OOP
class IsEveryOneHere
Programming in C++ - Module 11
{
Isolate the error in
private:
this code: ????
int iNum;
public:
void TotalStudents()
{
iNum = 164;
cout << "Total Students Are: " << iNum << endl;
}
};
int main()
{
InYourDreams IsEveryOneHere;
InYourDreams:TotalStudents();
system("Pause");
return 0;
}
foad - Curtin, Sarawak
Slide 17 of 66
Introduction to OOP
Slide 14 of 66
Introduction to OOP
class IsEveryOneHere
Programming in C++ - Module 11
{
Isolate the error in
private:
this code: ????
int iNum = 164;
public:
void TotalStudents()
{
iNum = 164;
cout << "Total Students Are: " << iNum << endl;
}
};
int main()
{
IsEveryOneHere InYourDreams;
InYourDreams.TotalStudents();
system("Pause");
return 0;
}
foad - Curtin, Sarawak
Slide 16 of 66
Introduction to OOP
class IsEveryOneHere
Programming in C++ - Module 11
{
Isolate the error in
private:
this code: ????
int iNum;
public:
void TotalStudents()
{
iNum = 164;
cout << "Total Students Are: " << iNum << endl;
}
};
int main()
{
IsEveryOneHere InYourDreams;
InYourDreams.TotalStudents();
system("Pause");
return 0;
}
foad - Curtin, Sarawak
Slide 18 of 66
Introduction to OOP
int main()
Programming in C++ - Module 11
{
Isolate the error in
IsEveryOneHere InYourDreams;
this code: ????
InYourDreams.TotalStudents();
system("Pause");
return 0;
}
class IsEveryOneHere
{
private:
int iNum;
public:
void TotalStudents()
{
iNum = 164;
cout << "Total Students Are: " << iNum << endl;
}
};
foad - Curtin, Sarawak
Slide 19 of 66
Introduction to OOP
Objects.
Classes.
Data Abstraction.
Data Encapsulation.
Inheritance.
Polymorphism.
Dynamic Binding.
Message Passing.
Slide 20 of 66
Introduction to OOP
Classes are user-defined data types that behave like the builtin types of a programming language. Classes have member
data and member functions and they define which Object can
access them and how.
Slide 21 of 66
Introduction to OOP
Slide 22 of 66
Introduction to OOP
Slide 23 of 66
Introduction to OOP
Slide 24 of 66
Introduction to OOP
Slide 25 of 66
Introduction to OOP
Slide 26 of 66
Introduction to OOP
Slide 27 of 66
Introduction to OOP
Slide 28 of 66
Introduction to OOP
Slide 29 of 66
Introduction to OOP
Slide 30 of 66
Introduction to OOP
Slide 31 of 66
Introduction to OOP
Slide 32 of 66
7. Constructors
7. Constructors - Example
class add
{
private:
int num1, num2, num3;
public:
add(); //Constructor
void input(int, int);
void sum();
void disp();
};
declaration.
Constructors are used to initialize Member Data
A Constructor is a special function that is the member of a
Class
A Constructor has the same name as that of the class
If no Constructors are declared for a class, the compiler
Slide 33 of 66
Introduction to OOP
class add
{
int Num1,Num2;
public:
add()
{
Num1=Num2=0;
cout << Constructor Invoked;
}
};
Slide 35 of 66
Slide 34 of 66
Introduction to OOP
Introduction to OOP
Introduction to OOP
#include<iostream.h>
class Product //Program Without Constructor
{
public:
void Display(int N1, int N2)
{
cout << "The Product is: " << N1 * N2 <<endl;
}
};
void main()
{
int Num1, Num2;
cout << "Pls Enter Two Numbers: "<<endl;
cin >> Num1 >> Num2;
Product Prod;
Prod.Display(Num1, Num2);
}
Slide 36 of 66
Introduction to OOP
Num1 = 4
Num2 = 7
Slide 37 of 66
Introduction to OOP
#include<iostream.h>
class Product //Program Without Constructor
{
public:
void Display(int N1, int N2)
{
cout << "The Product is: " << N1 * N2 <<endl;
}
};
void main()
{
int Num1, Num2;
cout << "Pls Enter Two Numbers: "<<endl;
cin >> Num1 >> Num2;
Product Prod;
Prod.Display(Num1, Num2);
}
Slide 38 of 66
Slide 39 of 66
N1 = 4
N2 = 7
Introduction to OOP
Num1 = 4
Num2 = 7
N1 = 4
N2 = 7
Introduction to OOP
#include<iostream.h>
class Product //Steps to introduce a Constructor in a program
{
public:
void Display Product(int N1, int N2) STEP 1
{
cout << "The Product is: " << N1 * N2 <<endl;
}
};
void main()
{
int Num1, Num2;
cout << "Pls Enter Two Numbers: "<<endl;
cin >> Num1 >> Num2;
Product Prod;
Prod.Display(Num1, Num2);
}
Slide 40 of 66
#include<iostream.h>
class Product //Steps to introduce a Constructor in a program
{
public:
void Display Product(int N1, int N2) STEP 2
{
cout << "The Product is: " << N1 * N2 <<endl;
}
};
void main()
{
int Num1, Num2;
cout << "Pls Enter Two Numbers: "<<endl;
cin >> Num1 >> Num2;
Product Prod;
Prod.Display(Num1, Num2);
}
#include<iostream.h>
class Product //Steps to introduce a Constructor in a program
{
public:
void Display Product(int N1, int N2)
{
cout << "The Product is: " << N1 * N2 <<endl;
}
};
void main()
{
int Num1, Num2;
cout << "Pls Enter Two Numbers: "<<endl;
cin >> Num1 >> Num2;
Product Prod;
Prod.Display(Num1, Num2); STEP 3
}
Slide 41 of 66
Num2 = 7
Num1 = 4
Introduction to OOP
Slide 42 of 66
Introduction to OOP
Introduction to OOP
#include<iostream.h>
class Product //Steps to introduce a Constructor in a program
{
public:
void Display Product(int N1, int N2)
{
cout << "The Product is: " << N1 * N2 <<endl;
}
};
void main()
{
int Num1, Num2;
cout << "Pls Enter Two Numbers: "<<endl;
cin >> Num1 >> Num2;
Product Prod(Num1, Num2); STEP 4
Prod.Display(Num1, Num2);
}
Slide 43 of 66
Introduction to OOP
#include<iostream.h>
class Square
{
private:
int num;
public:
Square()
{
cout << "Please Enter A Number: ";
cin >> num;
cout << "The Square of " << num << " is: " << num * num << endl;
}
};
void main()
{
Square Sq;
}
Slide 45 of 66
Introduction to OOP
Slide 47 of 66
Introduction to OOP
Slide 46 of 66
Introduction to OOP
Slide 44 of 66
7. Constructors
Questions:
Can we have more than one
Constructor in a Class?
Can Constructors be
overloaded?
Introduction to OOP
Slide 48 of 66
Introduction to OOP
7. Constructors
a default Constructor.
Module11-ConstructorExample1.pdf file
Module11-ConstructorExample2.pdf file
Module11-ConstructorExample3.pdf file
Slide 49 of 66
Introduction to OOP
Slide 50 of 66
Introduction to OOP
7. Destructors
Questions:
Constructors.
A Destructor is used to de-initialize objects and release
memory.
Destructors cannot be overloaded.
A Destructor is executed only after its function has finished
Slide 51 of 66
Introduction to OOP
Slide 52 of 66
Introduction to OOP
7. Destructors
7. Destructors An Example
int Num1,Num2;
public:
add()
{
Num1=Num2=0;
cout << Constructor Invoked;
Constructors.
}
~add()
{
memory.
Num1=Num2=0;
cout << Destructor Invoked
}
};
Slide 53 of 66
Introduction to OOP
Slide 54 of 66
Introduction to OOP
class JackAndJill
{Programming in C++ - Module 11
Predict the
public:
JackAndJill()
{
cout << "Jack And Jill Went Up The Hill!" << endl;
}
~JackAndJill()
{
cout << "To Fetch A Pail Of Pepsi!" << endl;
}
};
void Func()
{
JackAndJill Kill;
cout << "Another Nursery Rhyme: " << endl;
}
int main()
{
Func();
system("PAUSE");
return 0;
foad - Curtin, Sarawak
Slide 55 of 66
}
output:
Summary
OOP is a new paradigm in programming where data and
of code.
The Access Specifiers of a class can be private, public or
protected.
Introduction to OOP
Slide 56 of 66
Introduction to OOP
Summary
Summary
class name.
A constructor is mainly used to initialize data.
Slide 57 of 66
Introduction to OOP
Slide 58 of 66
Introduction to OOP
Revision
Revision
Slide 59 of 66
Introduction to OOP
Slide 60 of 66
Introduction to OOP
10
Revision
Slide 61 of 66
Introduction to OOP
Slide 62 of 66
Introduction to OOP
Revision
Revision
Slide 63 of 66
Introduction to OOP
Modify the program given below to include a constructor. The output of the
program should not be altered.
#include<iostream.h>
class RusselPeters
{
public:
void Said()
{
cout << "Some Body Is Gonna Get A Hurting!" <<endl;
}
};
void main()
{
RusselPeters Father;
Father.Said();
}
Slide 65 of 66
Introduction to OOP
Revision
Slide 64 of 66
Introduction to OOP
The End
Did you know:
Bjarne Stroustrup was born in Arhus, Denmark. He was a good
student at School and went on to finish his Masters Degree in
Mathematics from University of Arhus. He eventually got his PhD
in Computer Science from Cambridge University, England.
Slide 66 of 66
Introduction to OOP
11