CSC241: Object Oriented Programming

Slides:



Advertisements
Similar presentations
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Advertisements

1 CSC241: Object Oriented Programming Lecture No 21.
Classes: A Deeper Look Systems Programming.  constconst  const objects and const member functions   Composition   Friendship  this  this pointer.
Class and Objects.
Classes: A Deeper Look Systems Programming.
A Deeper Look at Classes CS-2303, C-Term A Deeper Look at Classes CS-2303 System Programming Concepts (Slides include materials from The C Programming.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Review of C++ Programming Part II Sheng-Fang Huang.
More About Classes Chapter Instance And Static Members instance variable: a member variable in a class. Each object has its own copy. static variable:
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
1 CSC241: Object Oriented Programming Lecture No 13.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
Learners Support Publications Classes and Objects.
1 CSC241: Object Oriented Programming Lecture No 06.
Chapter 14 More About Classes. Chapter 13 slide 2 Topics 13.1 Instance and Static Members 13.2 Friends of Classes 13.3 Memberwise Assignment 13.4 Copy.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Classes: A Deeper Look Part.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Visual C# 2012 for Programmers © by Pearson Education, Inc. All Rights Reserved.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 23 November 19, 2009.
LECTURE 4. Composition Definition: A data member of a class is an object of some other class Example: an AlarmClock object needs to know when it is supposed.
Chapter 17 - C++ Classes: Part II Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
1 CSC241: Object Oriented Programming Lecture No 02.
1 CSC241: Object Oriented Programming Lecture No 11.
C++ Lecture 5 Monday, 18 July Chapter 7 Classes, continued l const objects and const member functions l Composition: objects as members of classes.
1 CSC241: Object Oriented Programming Lecture No 05.
Programming II Array of objects. this Using the this Pointer this Objects use the this pointer implicitly or explicitly. – this is – this is used implicitly.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
Programming Techniques Classes II Important Class Features Spring 2009.
Learning Objectives Fundamentals of Operator Overloading. Restrictions of Operator Overloading. Global and member Operator. Overloading Stream-Insertion.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 14: More About Classes.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Eine By: Avinash Reddy 09/29/2016.
Procedural and Object-Oriented Programming
Visit for more Learning Resources
Chapter 13: Overloading and Templates
Chapter 14: More About Classes.
Lecture 5: Classes (continued) Feb 1, 2005
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
CSC241: Object Oriented Programming
CSC241: Object Oriented Programming
CISC181 Introduction to Computer Science Dr
Introduction to Classes
Structures Revisited what is an aggregate construct? What aggregate constructs have we studied? what is a structure? what is the keyword to define a structure?
Chapter 15: Overloading and Templates
Chapter 17 - C++ Classes: Part II
Chapter 14: More About Classes.
Introduction to Classes
Visit for more Learning Resources
FUNCTIONS& FUNCTIONS OVERLOADING
10.2 const (Constant) Objects and const Member Functions
Dr. Bhargavi Dept of CS CHRIST
Classes and Objects.
Object Oriented Programming Using C++
Classes: A Deeper Look, Part 2
9-10 Classes: A Deeper Look.
Submitted By : Veenu Saini Lecturer (IT)
Recitation Course 0520 Speaker: Liu Yu-Jiun.
Recitation Course 0603 Speaker: Liu Yu-Jiun.
Operator Overloading; String and Array Objects
Classes: A Deeper Look, Part 2
A Deeper Look at Classes
Objects as Function Arguments
9-10 Classes: A Deeper Look.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Presentation transcript:

CSC241: Object Oriented Programming Lecture No 04

Previous Lecture Constructor – example program Placing class in separate file Destructor – example program Constructor with arguments – example program

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

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

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

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)

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.

Classes, Objects, and Memory

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

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

Example – Time class Data members Member functions program

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;

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

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

Example Go to program

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

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

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

Example – Count class Go to program

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

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

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

Implicitly and Explicitly Use this Pointer Go to program

Cascading function calls using this pointer Multiple member functions are invoked in the same statement

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

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

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

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

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

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

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

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