Visit for more Learning Resources

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Contents o Introduction o Characteristics of Constructor. o Types of constructor. - Default Constructor - Parameterized Constructor - Copy Constructor.
Chapter 7: User-Defined Functions II
C++ Programming: Program Design Including Data Structures, Third Edition Chapter 7: User-Defined Functions II.
Road Map Introduction to object oriented programming. Classes
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to Classes and Objects Outline Introduction Classes, Objects, Member Functions and Data.
1 Building Classes (the "++" in C++) (Chapter 14) Representing More Complex Objects.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Classes Representing Non-Trivial Objects. Problem Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Class Miscellanea Details About Classes. Review We’ve seen that a class has two sections: class Temperature { public: //... public members private: //...
Chapter 3 Part I. 3.1 Introduction Programs written in C ◦ All statements were located in function main Programs written in C++ ◦ Programs will consist.
1 CSC241: Object Oriented Programming Lecture No 02.
2 Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Copyright © 2002 W. A. Tucker1 Chapter 10 Lecture Notes Bill Tucker Austin Community College COSC 1315.
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?
1 CSC241: Object Oriented Programming Lecture No 05.
1 Mr. Muhammad Hanif Lecturer Information Technology MBBS Campus Dadu University of SIndh.
1 CSC241: Object Oriented Programming Lecture No 03.
1 Introduction to Object Oriented Programming Chapter 10.
Structure A Data structure is a collection of variable which can be same or different types. You can refer to a structure as a single variable, and to.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Class Operations Creating New Types. Review Last time, we began building, a class to allow us to model temperatures: Last time, we began building Temperature,
Constructors and Destructors
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
Structures and Classes
User-Written Functions
Chapter 7: User-Defined Functions II
Overloaded Constructors and Multiple 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?
CSC241: Object Oriented Programming
Abstract Data Types Programmer-created data types that specify
Review What is an object? What is a class?
CSC241: Object Oriented Programming
Review: Two Programming Paradigms
Chapter 3: Using Methods, Classes, and Objects
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 5 Classes.
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Lecture 4-7 Classes and Objects
Introduction to Classes
Visit for more Learning Resources
FUNCTIONS& FUNCTIONS OVERLOADING
6 Chapter Functions.
Learning Objectives Classes Constructors Principles of OOP
Constructors and Destructors
Classes and Objects.
Object Oriented Programming Using C++
Classes and OOP.
CLASSES AND OBJECTS.
9-10 Classes: A Deeper Look.
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
ENERGY 211 / CME 211 Lecture 17 October 29, 2008.
Standard Version of Starting Out with C++, 4th Edition
Objects as Function Arguments
Constructors & Destructors
9-10 Classes: A Deeper Look.
四時讀書樂 (春) ~ 翁森 山光照檻水繞廊,舞雩歸詠春風香。 好鳥枝頭亦朋友,落花水面皆文章。 蹉跎莫遣韶光老,人生唯有讀書好。
Functions Chapter No. 5.
Introduction to Classes and Objects
Presentation transcript:

Visit for more Learning Resources Classes (Part I) Visit for more Learning Resources

Contents Building Classes Defining Objects Constructors Using Default Parameters in Functions Objects as Function Arguments Returning Objects from Functions

Problem Statement Write a program that reads a temperature (either Fahrenheit or Celsius), and displays that same temperature in both scales

Preliminary Analysis An object must be directly representable using a single type A temperature has two attributes: its magnitude (a double), and its scale (a character) When an object cannot be directly represented by any of the available types, build a class!

Building Classes Begin by defining variables to store the attributes of the object being represented (a temperature) double magnitude; char scale;

Building Classes We then wrap these variables in a class declaration: class Temperature { public: private: double magnitude; char scale; }; When declared within a class declaration, such variables are called class data members Declaring this class does not allocate memory for a Temperature It just tells the compiler what temperature is and what are its attributes

Data Hiding Hiding data from parts of the program that do not need to access it Classes have a public section and a private section Items (data or functions) declared in the public section are accessible to users of the class i.e., they can be accessed from outside the class Items declared in the private section are inaccessible to users of the class i.e., they can only be accessed from within the class Data members should go in the private section, to prevent programmers from writing programs that access data members directly All class members are private by default

Defining an Object A programmer can now write: Temperature aTemp; and object aTemp can be visualized as follows: magnitude scale aTemp The data members magnitude and scale within aTemp are uninitialized

Classes versus Objects An object has the same relationship to a class as a variable has to a data type An object is said to be an instance of a class As you don’t assign values to types rather to variables int = 5; //wrong int x = 5; //right Similarly an object of a class has to be created before it can be assigned any values Temperature aTemp; aTemp.magnitude = 37.0;

Member Functions Operations on a class object are usually implemented as class member functions A call to a member function: Object.Function() can be thought of as the caller sending the object Object a message named Function. The definition of a function member details what Object does in response to the message.

Member Functions Let’s say we want to display the values of a Temperature Object using a display function aTemp.display(); void Temperature::display() { cout << “Magnitude:” <<magnitude ; cout <<“ ,Scale:” << scale; } The function returns nothing, so its return-type is void The full name Temperature::display() tells the compiler this is a Temperature member function

Member Function Prototypes class Temperature { public: void display(); private: double magnitude; char scale; }; By declaring the prototype within the class, we tell the compiler that class Temperature has a function member named display() Most function prototypes go in the class public section

Problem At present, a Temperature declaration: Temperature aTemp; leaves aTemp’s data members uninitialized To auto-initialize them to a default value (e.g., 0C), we can use a special function called a constructor

Constructors A constructor function is a class member function whose task is to initialize the class data members Temperature::Temperature() { magnitude = 0.0; scale = ‘C’; } Since it returns nothing, a constructor has no return type (not even void) The name of a constructor is always the name of the class (in this case Temperature()) As a member function of class Temperature, its full name is Temperature::Temperature()

Constructor Prototype class Temperature { public: Temperature(); void display(); private: double magnitude; char scale; }; Since they specify the first thing a user of the class needs to know (i.e., how to define class objects), constructor prototypes are usually the first function members listed in the public section of the class

Object Definitions A programmer can now write: Temperature aTemp; and object aTemp can be visualized as follows: C magnitude scale aTemp 0.0 The class constructor is automatically called whenever a class object is defined

Problem 2 At present, we can only initialize a Temperature to a default value: Temperature aTemp; We have no means of initializing a Temperature to any other value. To initialize a Temperature to a particular value (e.g., 98.6F), we can overload the constructor with a second definition

Constructor 2 Definition To overload the constructor, we just provide a second definition (and prototype) that differs from all other constructors in at least one parameter To initialize the data members of our class, this second constructor must receive the initial values via its parameters Temperature::Temperature(double mag, char sc) { assert(sc == ‘F’ || sc == ‘C’); magnitude = mag; scale = sc; }

Constructor 2 Prototype class Temperature { public: Temperature(); Temperature(double mag, char sc); void display(); private: double magnitude; char scale; }; The same name can be used to define different functions, provided the signature (the list of the parameter types) of each function is different

Object Definitions A programmer can now write: Temperature temp1, temp2(98.6, ‘F’); and temp1 and temp2 are defined as follows: C magnitude scale temp1 0.0 F temp2 98.6 The compiler uses the number of arguments in a declaration to decide which constructor to use in initializing an object

Testing To test this, we can write in main() int main() { Temperature temp1, temp2(98.6, ‘F’); temp1.display(); // displays Magnitude:0,ScaleC cout << endl; temp2.display(); // displays Magnitude:98.6,Scale:F }

Placing function definition within class definition You can also place the definition of a function into the declaration of the class which automatically makes the function inline class Temperature { private: double magnitude; char scale; public: Temperature() { magnitude = 0.0; scale = ‘C’; } void display() { cout << “Magnitude:” <<magnitude ; cout <<“ ,Scale:” << scale; };

Another way to define a constructor – the preferred approach class Temperature { private: double magnitude; char scale; public: Temperature(): magnitude (0.0), scale(‘C’) { /*empty body*/ } Temperature(double mag, char sc): magnitude(mag),scale(sc) {assert(sc ==‘F’ || sc == ‘C’} void display() };

No-Arg Constructor If you don’t explicitly define any constructors, the compiler automatically generates a default constructor that takes no arguments So in case you do not define any constructors and simply create a Temperature object Temperature aTemp; The compiler will call a default constructor which does not, however, assign any values to the data members But once you’ve defined even one kind of constructor (may be a 2-arg constructor),the comp[iler will no longer create a default no-arg constructor for you

No-Arg Constructor E.g., if I remove the following from my class declaration Temperature(): magnitude (0.0), scale(‘C’) { /*empty body*/ } And then try to execute the following in main() Temperature t1; Temperature t2(98.6,’F’); This will give a compile time error

Default Arguments/Parameters Any C++ function can provide default values for its input function arguments If a value is not specified in a function call, the default value is assigned and sent to the called function To clear up any confusion to the compiler, a rule is enforced when using default values and multiple arguments All arguments to the right of an argument with a default value must be specified In other words, their can be no "holes" in your default parameter list This rule helps the compiler determine which arguments to assign values to in a function call

Using Default Parameters We can “combine” two functions (constructor without parameters and constructor with parameters) using default parameters of C++ Constructor for the Temperature class The prototype in the class definition would be Temperature(double mag=0.0, char sc='C'); The function definition would be Temperature::Temperature(double mag,char sc) { assert(sc == 'F' || sc == 'C'); magnitude = mag; scale = sc; }

Using Default Parameters In our main function, we can write: int main() { Temperature t1; Temperature t2(100.0); Temperature t3(100.0,'F'); Temperature t4 = Temperature(37.0,’C’); t1.display(); t2.display(); t3.display(); getch(); return 0; }

Objects as Function Arguments Class Distance { private: int feet; float inches public: Distance() : feet(0),inches(0.0) { } Distance(int ft,float in):feet(ft),inches(in)

Objects as Function Arguments void getdist ( ) { cout <<“\nEnter feet : “; cin >> feet; cout <<“\nEnter inches : “; cin >> inches; } void showdist ( ) cout <<feet << “ \‘ - << inches <<“\” “; void add_dist(Distance, Distance); };

Objects as Function Arguments 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;

Objects as Function Arguments void main( ) { Distance dist1, dist3; Distance dist2(10, 3.5); dist1.getdist( ); dist3.add_dist(dist1, dist2); cout <<“\ndist1 = “ ; dist1.showdist( ); cout <<“\ndist2 = “ ; dist2.showdist( ); cout <<“\ndist3 = “ ; dist3.showdist( ); }

Returning Objects from Functions Distance Distance :: add_dist(Distance d2) { 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; int main() Distance dist1,dist2; Distance dist3(11,6.5); dist1.getdist(); //from user dist2 = dist1.add_dist(dist3); return 0;

For more detail contact us Compulsory Reading Robert Lafore, Chapter 6: Objects and Classes For more detail contact us