Download presentation
Presentation is loading. Please wait.
1
CSC241: Object Oriented Programming
Lecture No 04
2
Previous Lecture Constructor – example program
Placing class in separate file Destructor – example program Constructor with arguments – example program
3
Today’s Lecture Overloaded function const (constant) friend function
Constructor const (constant) object member function data member object as function argument friend function this pointer
4
Objects as Function Arguments
class Distance { //Distance class private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) void getdist(){ cout << “\nEnter feet: “; cin >> feet; cout << “Enter inches: “; cin >> inches; } void showdist(){ cout << feet << “:” << inches<<endl ; void add_dist( Distance, Distance ); }; void Distance::add_dist(Distance d2, Distance d3) { inches = d2.inches + d3.inches; feet = 0; if(inches >= 12.0) { inches -= 12.0; feet++; } feet += d2.feet + d3.feet; main() { Distance dist1, dist3; Distance dist2(11, 6.5); dist1.getdist(); dist3.add_dist(dist1, dist2); cout << “\ndist1 = “; dist1.showdist(); cout << “\ndist2 = “; dist2.showdist(); cout << “\ndist3 = “; dist3.showdist(); }
5
Overloaded Constructors
It’s convenient to be able to give variables of type Distance a value when they are first created Distance dist2(11, 6.25); which defines an object, and initializes it to a value of 11 for feet and 6.25 for inches. Distance dist1, dist2; then No-argument constructor is called/invoked (the default constructor) Since there are now two constructors with the same name, Distance(), we say the constructor is overloaded
6
Member Functions Defined Outside the Class
Such functions, needs to have a prototype/declaration within the class The function name, add_dist(), is preceded by the class name, Distance, and a new symbol—the double colon (::). This symbol is called the scope resolution operator. It is a way of specifying what class something is associated with In this situation, Distance::add_dist() means “the add_dist() member function of the Distance class” void Distance::add_dist(Distance d2, Distance d3)
7
Structures and Classes
We used structures as a way to group data and classes as a way to group both data and functions In fact, you can use structures in almost exactly the same way that you use classes The only formal difference between class and struct is that in a class the members are private by default, while in a structure they are public by default.
8
Classes, Objects, and Memory
9
const (constant) object
Some objects need to be modifiable and some do not Keyword const is used to specify that an object is not modifiable Any attempt to modify the object should result in a compilation error declares a const object noon of class Time and initializes it to 12 noon
10
const (constant) member function
Constant member function will not allow to modify private data member class aClass { private: int alpha; public: void nonFunc() //non-const member function { alpha = 99; } //OK void conFunc() const //const member function { alpha = 99; } //ERROR: can’t modify a member };
11
Example – Time class Data members Member functions program
12
const Objects as Function Arguments
class Distance { //Distance class private: int feet; float inches; public: Distance() : feet(0), inches(0.0) { } Distance(int ft, float in) : feet(ft), inches(in) void getdist(){ cout << “\nEnter feet: “; cin >> feet; cout << “Enter inches: “; cin >> inches; } void showdist(){ cout << feet << “:” << inches<<endl ; Distance add_dist(const Distance&) const; }; Distance Distance::add_dist(const Distance& d2) const { Distance temp; inches = 0; temp.inches = inches + d2.inches; if(temp.inches >= 12.0) { temp.inches -= 12.0; temp.feet = 1; } temp.feet += feet + d2.feet; return temp;
13
Cont. main() { Distance dist1, dist3; Distance dist2(11, 6.25); dist1.getdist(); dist3 = dist1.add_dist(dist2); cout << "\ndist1 = "; dist1.showdist(); cout << "\ndist2 = "; dist2.showdist(); cout << "\ndist3 = "; dist3.showdist(); } feet inches dist1 0.0 feet inches dist3 0.0 5 17 7.0 1.5 feet inches dist2 d2 11 6.5 Distance Distance::add_dist(const Distance& d2) const { Distance temp; temp.inches = inches + d2.inches; if(temp.inches >= 12.0) { temp.inches -= 12.0; temp.feet = 1; } temp.feet += feet + d2.feet; return temp; void getdist(){ cout << “\nEnter feet: “; cin >> feet; cout << “Enter inches: “; cin >> inches; } feet inches temp void showdist(){ cout << feet << “:” << inches<<endl ; } 1 17 13.5 1.5 dist1 = 5 : 7.0 dist2 = 11 : 6.5 Go to program dist3 = 17 : 1.5
14
Initializing a const Data Member
Constant data member must be initialized using member initializer list private: int count; const int increment; const data member must be initialized as soon as the object is created
15
Example Go to program
16
Common errors while using const
Following are compilation errors Defining as const a member function that modifies a data member of an object Defining as const a member function that calls a non-const member function of the class on the same instance of the class Invoking a non-const member function on a const object
17
friend Functions A friend function of a class is defined outside that class's scope It has the right to access the non-public (and public) members of the class Prototypes for friend functions appear in the class definition but friends are not member functions They are called just like a normal functions
18
Cont. Friend function can be declaration can be placed any where in class definition Place all friendship declarations first inside the class definition's body
19
Example – Count class Go to program
20
Using this pointer Object's member functions can manipulate the object's data How do member functions know which object's data members to manipulate? Every object has access to its own address through a pointer called this this pointer is passed (by the compiler) as an implicit argument to each of the object's non-static member functions
21
Cont. An object's this pointer is not part of the object itself
The size of the memory occupied by the this pointer is not reflected in the result of a sizeof operation on the object Objects use this pointer implicitly or explicitly to reference their data members and member functions
22
Type of this pointer The type of the this pointer depends on the type of the object non constant member function of class Employee, the this pointer has type Employee * const constant member function of the class Employee, the this pointer has the data type const Employee * const
23
Implicitly and Explicitly Use this Pointer
Go to program
24
Cascading function calls using this pointer
Multiple member functions are invoked in the same statement
25
Cont. Why does the technique of returning *this as a reference work?
The dot operator (.) associates from left to right so line 14 first evaluates t.setHour( 18 ) then returns a reference to object t The remaining expression is then interpreted as t.setMinute( 30 ).setSecond( 22 ); t.setMinute( 30 ) call executes and returns a reference to the object t. t.setSecond( 22 ); Go to program
26
static Data Members Each object of a class has its own copy of all the data members of the class In certain cases, only one copy of a variable should be shared by all objects of a class A static data member is used If a data item in a class is declared as static, only one such item is created for the entire class, no matter how many objects there are
27
Cont. A member static variable is visible only within the class, but its lifetime is the entire program It continues to exist even if there are no objects of the class A static data item is useful when all objects of the same class must share a common item of information
28
Uses of static data member
Suppose an object needed to know how many other objects of its class were in the program In a road-racing game, for example, a race car might want to know how many other cars are still in the race In this case a static variable count could be included as a member of the class All the objects would have access to this variable
29
Example – Race class Total car = 2, Car No. 10
class Race { private: static int count; int carNo; public: Race() { count++; carNo=0; } void setCarNo(int no) { carNo = no; } void printData() { cout<<“Total car = ”<< count; cout<<“,Car No. = ”<<carNo<<endl; } }; int Race::count = 0; main() Race c1, c2, c3; //create three objects c1.setCarNo(10); c2.setCarNo(11); c3.setCarNo(12); c1.printData(); c2.printData(); c3.printData(); Example – Race class Total car = 2, Car No. 10 Total car = 2, Car No. 11 Total car = 3, Car No. 12 main() { Race c1, c2; //create three objects c1.setCarNo(10); c2.setCarNo(11); c1.printData(); c2.printData(); Race c3; c3.setCarNo(12); c3.printData(); } Total car = 3, Car No. 10 Total car = 3, Car No. 11 Total car = 3, Car No. 12
30
Composition – Objects as Members of Classes
An AlarmClock object needs to know when it is supposed to sound its alarm So why not include a Time object as a member of the AlarmClock class It is refer as has-a relationship We will see how an object's constructor can pass arguments to member-object constructors
31
Example program Date.h Date class Employee.h Employee class
firstname lastname manager Date.h Date class Employee.h Employee class Date birthday Date hiredate birthday day month year hireday day month year Source_emp.cpp Employee manager Go to program
33
Dynamic Memory Management
C++ enables programmers to control the allocation and deallocation of memory in a program for any built-in or user-defined type Performed with operators new and delete
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.