0% found this document useful (0 votes)
15 views20 pages

Lecture 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views20 pages

Lecture 5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Object Oriented Programming

(CEN2012)
Week 03 Lecture 02
Dr. Muhammad Umair Khan
Assistant Professor
Department of Computer Engineering
National University of Technology
Classes Example
• Write a class Marks with three data members to store three marks.
Write three member functions in() to input marks, sum() to calculate
and return the sum and avg() to calculate and return the average
marks.
Classes Example
Classes Example
• Write a class Book with three datamembers BookID, Pages and Price.
It should also have following member functions.
• The get() function to input values.
• The show() function to display values.
• The set() function to set the values using parameters.
• The getprice() to return the value of price.
• The program to show the detail of the costly book.
Classes Example
Classes Example
Classes Example
• Write a class Result that contains roll no, name and marks of three
subjects. The marks are stored in an array of integers. The class also
contains the following data members.
• The input() function to input values in data members.
• The show() function to display values.
• The total() function to return the total marks of student.
• The avg() function to return the average marks of student.
Classes Example
Classes Example
Defining Member functions outside Class
Show the max and min
value in the array.
Defining Member functions outside Class
Initializing Objects with Constructors
• Each class can provide a constructor that can be used to initialize an
object of the class when the object is created.
• A constructor is a special member function that must be defined with
the same name as the class, so that the compiler can distinguish it
from the class’s other member functions.
• Normally, constructors are declared public.
Initializing Objects with Constructors (cont.)
• C++ requires a constructor call for each object that is created, which
helps ensure that each object is initialized before it’s used in a
program.
• The constructor call occurs implicitly when the object is created.
• If a class does not explicitly include a constructor, the compiler
provides a default constructor—that is, a constructor with no
parameters.
Constructors
Passing parameters to Constructor
Constructor Overloading
Destructors

You might also like