Você está na página 1de 11

Programming in C++ - Module 11

Programming in C++ - Module 11

Objectives

Module 11
Introduction
To
Object Oriented Programming

foad - Curtin, Sarawak

Slide 1 of 66

Introduction to OOP

Programming in C++ - Module 11

At the end of this session you will be able to:


1. Comprehend OOP And Its Benefits.
2. Comprehend Access Specifiers.
3. Use OOP In C++ Programs.
4. Comprehend Basic concepts of OOP.
5. Differentiate Between Structures & Classses.
6. Use Scope Resolution Operator :: in programs.
7. Comprehend Constructors & Destructors.

foad - Curtin, Sarawak

Slide 2 of 66

Introduction to OOP

Programming in C++ - Module 11

1. Traditional Procedural Programming

Question

Main function

What do we find inside a program

code?

Function 1

Function 2

Function 4

1. Variables

Function 5

2. Functions
Function 6

foad - Curtin, Sarawak

Function 3

Slide 3 of 66

Introduction to OOP

Programming in C++ - Module 11

foad - Curtin, Sarawak

Function 7

Function 8

Slide 4 of 66

Introduction to OOP

Programming in C++ - Module 11

1. Introduction to OOP

1. Introduction to OOP continued

 The fundamental idea behind OOP is to combine into a

 Object-Oriented Programming Approach


 Components of a class:
 Member Data
 Member Functions

single unit both Data and Functions that operate on the


data.
 Data and Functions are encapsulated into a single entity

known as a Class.

Member data

 The only way to access the Data and Functions in a Class

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

foad - Curtin, Sarawak

Slide 6 of 66

Introduction to OOP

Programming in C++ - Module 11

Programming in C++ - Module 11

1. Introduction to OOP An Analogy

1. Introduction to OOP Benefits

BMW, Ferrari, Chevrolet and Kancil share certain similar


features and hence can be grouped to form the objects of
the class Cars.
Similarly Table, Chair and Cupboard can be grouped to
form the objects of the class Furniture.

Characteristics of an Object-Oriented Language:


Class
Inheritance
Reusability of code
Polymorphism

foad - Curtin, Sarawak

Slide 7 of 66

Introduction to OOP

Programming in C++ - Module 11

foad - Curtin, Sarawak

Slide 8 of 66

Introduction to OOP

Programming in C++ - Module 11

2. Acsess Specifiers

3. Examples Demonstrating OOP

 The body of a class can contain 3 kinds of keywords:

Check

 Private: Contents of the class can be used only within

Module-11-IntroductionToOOP-ClassExamples.pdf file

the class.
for OOP coding Examples.
 Public: Contents of the class can be accessed from

outside the class.


 Protected: Contents of the class can be accessed only

by the derived class.


foad - Curtin, Sarawak

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

Programming in C++ - Module 11

foad - Curtin, Sarawak

Slide 11 of 66

foad - Curtin, Sarawak

Slide 10 of 66

Introduction to OOP

Programming in C++ - Module 11


#include<iostream.h> // Program Without Class
void Quiz(int Value)
{
if(Value = 2)
{cout << "Correct!" << endl;}
else
{cout << "Not Correct!" << endl;}
}
void main()
{
int Ans;
cout << "Who Wrote The Count of Monte Cristo?" << endl;
cout << "1.Charles Dickens." << endl;
cout << "2.Alexander Dumass." << endl;
cin >> Ans;
Quiz(Ans);
}
Introduction to OOP

foad - Curtin, Sarawak

Slide 12 of 66

Introduction to OOP

class IsEveryOneHere // Class Example


Programming in C++ - Module 11
{
private:
int iNum;
public:
void TotalStudents()
{
iNum = 164;
cout << "Total Students Are: " << iNum << endl;
}
};
int main()
{
IsEveryOneHere InYourDreams;
InYourDreams.TotalStudents();
system("Pause");
return 0;
}

Programming in C++ - Module 11


#include<iostream.h> // Program With Class
class Question
{
public:
void Quiz(int Value)
{
if(Value = 2)
{cout << "Correct!" << endl;}
else
{cout << "Not Correct!" << endl;}
}
};
void main()
{
int Ans;
cout << "Who Wrote The Count of Monte Cristo?" << endl;
cout << "1.Charles Dickens." << endl;
cout << "2.Alexander Dumass." << endl;
cin >> Ans;
Question Obj;
Obj.Quiz(Ans);
}
foad - Curtin, Sarawak

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

foad - Curtin, Sarawak

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

Programming in C++ - Module 11

Programming in C++ - Module 11

4. Basic Concepts of OOP











Objects.
Classes.
Data Abstraction.
Data Encapsulation.
Inheritance.
Polymorphism.
Dynamic Binding.
Message Passing.

foad - Curtin, Sarawak

Slide 20 of 66

Introduction to OOP

Programming in C++ - Module 11

4. Basic Concepts of OOP Objects

4. Basic Concepts of OOP Classes

Objects are the basic run-time entities in an OOP system.


They may represent a person, a place, a bank account, a
table of data etc. They can also represent User-Defined data.
They occupy space in memory.

A Class consists of variables and functions. The Class of an


Object defines what attributes an Object has. The entire set of
data and code of an Object can be made a user-defined data
type with the help of a Class.

Each Object has the potential to access data (variables) and


code to manipulate the data (functions).

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.

foad - Curtin, Sarawak

Slide 21 of 66

Introduction to OOP

Programming in C++ - Module 11

foad - Curtin, Sarawak

Slide 22 of 66

Introduction to OOP

Programming in C++ - Module 11

4. Basic Concepts of OOP Data Abstraction

4. Basic Concepts of OOP Data Encapsulation

Abstraction refers to the act of representing essential features


without including the background details or explanations.

The wrapping of data and functions into a single unit namely a


Class is known as Encapsulation. The data is not accessible
to the outside code and only those functions that are wrapped
in the class can access it.

Since Classes use the concept of Data Abstraction , they are


known as Abstract Data Types (ADT).

This insulation of the data from direct access by the program


is called data hiding.

foad - Curtin, Sarawak

Slide 23 of 66

Introduction to OOP

foad - Curtin, Sarawak

Slide 24 of 66

Introduction to OOP

Programming in C++ - Module 11

Programming in C++ - Module 11

4. Basic Concepts of OOP Inheritance

4. Basic Concepts of OOP Polymorphism

Inheritance is the process by which Objects of one Class


acquire the properties of another Class.

Polymorphism means the ability to take more than one form.

The power of inheritance lies in the fact that all common


features of the Subclasses can be accumulated in the
Superclass.

foad - Curtin, Sarawak

Slide 25 of 66

Introduction to OOP

Programming in C++ - Module 11

An operation may exhibit different behavior in different


instances, depending on the types of data used in the
operation.

foad - Curtin, Sarawak

Slide 26 of 66

Introduction to OOP

Programming in C++ - Module 11

4. Basic Concepts of OOP Dynamic Binding

4. Basic Concepts of OOP Message Passing

Binding refers to the linking of a function call to the code to be


executed.

A message for an Object is a request for execution of a


function in a Class.

Dynamic Binding means that the code associated with a given


function call is not known until the time of call at the run-time.
This is associated with Polymorphism and Inheritance.

Message Passing involves specifying the name of the Object,


the name of the function and the information to be sent.

foad - Curtin, Sarawak

Slide 27 of 66

Introduction to OOP

Programming in C++ - Module 11

foad - Curtin, Sarawak

Slide 28 of 66

Introduction to OOP

Programming in C++ - Module 11

5. Structures & Classes

6. Scope Resolution Operator ::

The main difference between a Structure and a Class is:

This operator is used when we want to define the contents of


a class outside the body of the class.

 The contents of a Structure is public by default.

The member functions of a class are defined outside the class


using the scope resolution operator ::

 The contents of a Class is private by default.

foad - Curtin, Sarawak

Slide 29 of 66

Introduction to OOP

foad - Curtin, Sarawak

Slide 30 of 66

Introduction to OOP

Programming in C++ - Module 11

Programming in C++ - Module 11

6. Scope Resolution Operator ::

6. Scope Resolution Operator ::

//Program Without Scope Resolution Operator


#include<iostream.h>
class Average
{
double M1, M2;
public:
void Display(double Val1, double Val2)
{
M1 = Val1;
M2 = Val2;
cout << "Average is: " << (M1 + M2) / 2 << endl;
}
};
void main()
{
double Marks1, Marks2;
cout << "Please Enter First Marks: ";
cin >> Marks1;
cout << "Please Enter Second Marks: ";
cin >> Marks2;
Average Avg;
Avg.Display(Marks1, Marks2);
}

//Program With Scope Resolution Operator


#include<iostream.h>
class Average
{
double M1, M2;
public:
void Display(double, double);
};
void Average::Display(double Val1, double Val2)
{
M1 = Val1;
M2 = Val2;
cout << "Average is: " << (M1 + M2) / 2 << endl;
}
void main()
{
double Marks1, Marks2;
cout << "Please Enter First Marks: ";
cin >> Marks1;
cout << "Please Enter Second Marks: ";
cin >> Marks2;
Average Avg;
Avg.Display(Marks1, Marks2);
}

foad - Curtin, Sarawak

Slide 31 of 66

Introduction to OOP

Programming in C++ - Module 11

foad - Curtin, Sarawak

Slide 32 of 66

Programming in C++ - Module 11

7. Constructors

7. Constructors - Example

 Data members of a class cannot be initialized at the time of

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

invokes its own Constructor


foad - Curtin, Sarawak

Slide 33 of 66

Introduction to OOP

Programming in C++ - Module 11

class add
{
int Num1,Num2;
public:
add()
{
Num1=Num2=0;
cout << Constructor Invoked;
}
};

Slide 35 of 66

foad - Curtin, Sarawak

Slide 34 of 66

Introduction to OOP

Programming in C++ - Module 11

7. Constructors Example Initializing Data

foad - Curtin, Sarawak

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);
}

foad - Curtin, Sarawak

Slide 36 of 66

Introduction to OOP

Programming in C++ - Module 11


#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);
}

foad - Curtin, Sarawak

Programming in C++ - Module 11

Pls Enter Two Numbers:


4
7

Num1 = 4
Num2 = 7

Slide 37 of 66

Introduction to OOP

Programming in C++ - Module 11


#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);
}

foad - Curtin, Sarawak

#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);
}

foad - Curtin, Sarawak

Pls Enter Two Numbers:


4
7

Slide 38 of 66

Pls Enter Two Numbers:


4
7
The Product is 28

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);
}

foad - Curtin, Sarawak

Slide 40 of 66

Programming in C++ - Module 11

#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

Programming in C++ - Module 11

Programming in C++ - Module 11

foad - Curtin, Sarawak

Num1 = 4

Introduction to OOP

foad - Curtin, Sarawak

Slide 42 of 66

Introduction to OOP

Introduction to OOP

Programming in C++ - Module 11

Programming in C++ - Module 11

#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);
}

foad - Curtin, Sarawak

Slide 43 of 66

7. Constructors Program Without Constructor


#include<iostream.h>
class Square
{
private:
int num;
public:
void Display()
{
cout << "Please Enter A Number: ";
cin >> num;
cout << "The Square of " << num << " is: " << num * num << endl;
}
};
void main()
{
Square Sq;
Sq.Display();
}

Introduction to OOP

Programming in C++ - Module 11

#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

Programming in C++ - Module 11

Slide 47 of 66

Introduction to OOP

1.#include<iostream.h> //Program Without a Constructor


2.class Add
3.{
4. private:
5. int num1,num2,num3;
//Member data.
6. public:
7. void display(int var1, int var2)
//Member function.
8. {
9.
num1=var1;
10.
num2=var2;
11.
num3=num1+num2;
12.
cout<<"The sum of the two numbers is "<<num3<<endl;
13. }
14.};
15.void main()
16.{
17. Add A1;
18. int iX, iY;
19. cout<<"Input two numbers"<<endl;
20. cin>>iX;
21. cin>>iY;
22. A1.display(iX,iY);
23.}

foad - Curtin, Sarawak

Slide 46 of 66

Introduction to OOP

Programming in C++ - Module 11

1.#include<iostream.h> //Program Using a Constructor


2.class Add
3.{
4. private:
5. int num1,num2,num3;
//Member data.
6. public:
7. Add(int var1, int var2)
//Constructor
8. {
9.
num1=var1;
10.
num2=var2;
11.
num3=num1+num2;
12.
cout<<"The sum of the two numbers is "<<num3<<endl;
13. }
14.};
15.void main()
16.{
17. int iX, iY;
18. cout<<"Input two numbers"<<endl;
19. cin>>iX;
20. cin>>iY;
21. Add A1(iX,iY);
22.}

foad - Curtin, Sarawak

Slide 44 of 66

Programming in C++ - Module 11

7. Constructors Program With Constructor

foad - Curtin, Sarawak

foad - Curtin, Sarawak

7. Constructors
Questions:
Can we have more than one
Constructor in a Class?
Can Constructors be
overloaded?
Introduction to OOP

foad - Curtin, Sarawak

Slide 48 of 66

Introduction to OOP

Programming in C++ - Module 11

Programming in C++ - Module 11

7. Constructors

7. Examples Demonstrating Constructor

 C++ permits multiple Constructors to be defined for a class.

Check The Following Files:

 Any Constructor that does not receive parameters is called

a default Constructor.

Module11-ConstructorExample1.pdf file

 Constructors can be overloaded.

Module11-ConstructorExample2.pdf file

 Constructor Functions do not have a return type.

Module11-ConstructorExample3.pdf file

 Constructors are automatically executed when an object of

its class is created.


foad - Curtin, Sarawak

Slide 49 of 66

Introduction to OOP

Programming in C++ - Module 11

foad - Curtin, Sarawak

Slide 50 of 66

Introduction to OOP

Programming in C++ - Module 11

7. Destructors

Questions:

 A Destructor is a function which has the same name as its

Can we use the return keyword


in a Constructor?

class but is prefixed with a ~(tilde) character.


 Destructors are functions that are complimentary to

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

being executed itself.

foad - Curtin, Sarawak

Slide 51 of 66

Introduction to OOP

Programming in C++ - Module 11

foad - Curtin, Sarawak

Slide 52 of 66

Introduction to OOP

Programming in C++ - Module 11


Class add
{

7. Destructors

7. Destructors An Example
int Num1,Num2;

 A Destructor is a function which has the same name as its

public:

class but is prefixed with a ~(tilde) character.

add()
{

 Destructors are functions that are complimentary to

Num1=Num2=0;
cout << Constructor Invoked;

Constructors.
}
~add()
{

 A Destructor is used to de-initialize objects and release

memory.

Num1=Num2=0;
cout << Destructor Invoked

 Destructors cannot be overloaded.

}
};

foad - Curtin, Sarawak

Slide 53 of 66

Introduction to OOP

foad - Curtin, Sarawak

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:

Programming in C++ - Module 11

Summary
OOP is a new paradigm in programming where data and

functions are combined together in a construct called class.


OOP provides many benefits to programmers like reusability

of code.
The Access Specifiers of a class can be private, public or

protected.

Introduction to OOP

Programming in C++ - Module 11

foad - Curtin, Sarawak

Slide 56 of 66

Introduction to OOP

Programming in C++ - Module 11

Summary

Summary

Some essential concepts that make an OOP language are

Objects, Classes, Data Abstraction, Data Encapsulation,


Inheritance, Polymorphism, Dynamic Binding and Message
Passing.

A constructor is a function which has the same name as its

class name.
A constructor is mainly used to initialize data.

There is a fundamental difference between Structures and

Classes. The members of a Class are private by default


whereas the members of a Structure are public by default.
Scope Resolution Operator can be used to define a function

outside the boundary of a class.


foad - Curtin, Sarawak

Slide 57 of 66

Introduction to OOP

Programming in C++ - Module 11

A destructor is a function which has the same name as its

class preceded by a ~ character.


A destructor is used to de-initialize objects.

foad - Curtin, Sarawak

Slide 58 of 66

Introduction to OOP

Programming in C++ - Module 11

Revision

Revision

What does the keyword void signify when used with a


function?

Create a program which demonstrates User-defined Data


Types by using Structures.

(A) The function cannot take any values.


(B) The function does not hold any value.

Create a program which is an example of static Data Storage


Type.

(C) The function returns a value.


(D) None of the above.
foad - Curtin, Sarawak

Slide 59 of 66

Introduction to OOP

foad - Curtin, Sarawak

Slide 60 of 66

Introduction to OOP

10

Programming in C++ - Module 11

Programming in C++ - Module 11


Revision

Revision

Consider the following code:


#include<iostream.h>
class DoNotWorry
{
public:
void ItIsJustAnExam()
{
cout << "You Can Do It!" << endl;
}
};
Which of the following is the correct main function for the above code?
(A) void main() { DoNotWorry Not; }
(B) void main() { DoNotWorry A1; Not.ItIsJustAnExam(); }
(C) void main() { DoNotWorry Not; Not.ItIsJustAnExam(); }
(D) void main() { DoNotWorry Not; Not.void ItIsJustAnExam(); }

Which of the following is found inside a class?


(A) Member Data.
(B) Member Variable.
(C) Member Function.
(D) All of the above.
foad - Curtin, Sarawak

Slide 61 of 66

Introduction to OOP

Programming in C++ - Module 11

foad - Curtin, Sarawak

Slide 62 of 66

Introduction to OOP

Programming in C++ - Module 11

Revision

Revision

Which of the following is false with respect to a Constructor?


Which of the following is false with respect to a Destructor?
(A)A Constructor is a special function that is the member of a
class.

(A) Destructors are complimentary to constructors.

(B)A Constructor has the same name as the class name.

(B) Destructors can be overloaded.

(C)A Constructor should have the void return type only.

(C) Destructors release memory given by Constructors.

(D)A Constructor is used to initialize Member Data.

(D) Destructors are preceded by the tilde character.

foad - Curtin, Sarawak

Slide 63 of 66

Introduction to OOP

Programming in C++ - Module 11

foad - Curtin, Sarawak

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

Programming in C++ - Module 11

Revision

foad - Curtin, Sarawak

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.

foad - Curtin, Sarawak

Slide 66 of 66

Introduction to OOP

11

Você também pode gostar