Classes. Class expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation.

Slides:



Advertisements
Similar presentations
Constructors and Destructors. Constructor Constructor—what’s this? Constructor—what’s this? method used for initializing objects (of certain class) method.
Advertisements

Chapter 14: Overloading and Templates
OOP Spring 2007 – Recitation 21 Object Oriented Programming Spring 2007 Recitation 2.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
Constructors A constructor is a method that creates an object in Java. It does not return anything and it is not void. It’s only purpose is to create an.
Review of C++ Programming Part II Sheng-Fang Huang.
Computer Science and Software Engineering University of Wisconsin - Platteville 7. Inheritance and Polymorphism Yan Shi CS/SE 2630 Lecture Notes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
Inheritance. Recall the plant that we defined earlier… class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant(
Chapter 9 Defining New Types. Objectives Explore the use of member functions when creating a struct. Introduce some of the concepts behind object-oriented.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 13: Introduction to Classes.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Chapter 13. Procedural programming vs OOP  Procedural programming focuses on accomplishing tasks (“verbs” are important).  Object-oriented programming.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
ECE122 Feb. 22, Any question on Vehicle sample code?
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.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
CS212: Object Oriented Analysis and Design
Rina System development with Java Instructors: Rina Zviel-Girshin Lecture 4.
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.
Chapter 10: Classes and Data Abstraction. Objectives In this chapter, you will: Learn about classes Learn about private, protected, and public members.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and 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?
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
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.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
Array in C++ / review. An array contains multiple objects of identical types stored sequentially in memory. The individual objects in an array, referred.
Chapter 10: Classes and Data Abstraction. Classes Object-oriented design (OOD): a problem solving methodology Objects: components of a solution Class:
72 4/11/98 CSE 143 Abstract Data Types [Sections , ]
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 13: Introduction to Classes.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
OOP Basics Classes & Methods (c) IDMS/SQL News
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Pointer to an Object Can define a pointer to an object:
Procedural and Object-Oriented Programming
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?
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
7. Inheritance and Polymorphism
Review What is an object? What is a class?
Programming with ANSI C ++
Chapter 3: Using Methods, Classes, and Objects
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?
CS212: Object Oriented Analysis and Design
Templates.
The dirty secrets of objects
Understanding Inheritance
Introduction to Classes
Interfaces.
CIS 199 Final Review.
Class.
Java Programming Language
Lecture 8 Object Oriented Programming (OOP)
Presentation transcript:

Classes

Class expanded concept of a data structure: instead of holding only data, it can hold both data and functions. An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable. Usually class’s declarations will be in.h file, while its implementation will be in the.cpp file.

Define a plant class Plant { private: bool hasLeaves; doubleheight; }; class body access control Data Members Do we have a plant yet ? NO !!! We have a plan for a plant "Plant.h"

Create a plant #include "Plant.h" int main() { Plant myPlant; } Only after that line we have a plant ! “main.cpp" What does this plant look like? ? Did we initialized the members ? NO !!! Lets see what happened… WE DO NOT KNOW

Construction Plant myPlant; When we create an instance of a class – a constructor called. When we do not define any – a default constructor is created by the compiler. class Plant { public: Plant() { } private: bool hasLeaves; double height; }; But… It’s empty!!! Well – not exactly. It creates the members : First : hasLeaves Second: height Did we initialized the members ? NO !!! double and bool types contain TRASH by default

Construction class Plant { public: Plant() : hasLeaves( true ), height( 1 ) { } private: bool hasLeaves; double height; }; Constructor is a special function of a class. It’s name is the same as the class name. It has no return value It allows members initialization – when they are created. Like that: Now we KNOW: when we create a plant like this: Plant myPlant; We have a plant of height 1, and with leaves.

Construction class Plant { public: Plant() : hasLeaves( true ), height( 1 ) { } Plant( bool withLeaves, double theHeight ) : hasLeaves( withLeaves ), height (theHeight) { } private: bool hasLeaves; double height; }; What if we want to create a plant with some parametric member variables? Now we can create another plant ( in main() ): Plant myOtherPlant( false, 1 ); Now we have 2 constructors: Default: Plant() no parameters Another: Plant( bool withLeaves, double theHeight )

Construction What if have only non default constructor (or constructors)? class Plant { public: Plant( bool withLeaves, double theHeight ) : hasLeaves( withLeaves ), height (theHeight) { } private: bool hasLeaves; double height; }; Can we now create a plant like that? Plant myPlant; NO If we have at least one constructor – the compiler does not create a default one! We have to do it, IF we want to be able to create a default plant: Plant myPlant;

Member functions (methods) class Plant { public: Plant() : hasLeaves( true ), height( 1 ) { } Plant( bool withLeaves, double theHeight ) : hasLeaves( withLeaves ), height (theHeight) { } private: bool hasLeaves; double height; }; Right now we can only create a plant. But a class is also about behavior! Let’s add our plant some member functions.

Member functions (methods) class Plant { public: Plant() : hasLeaves( true ), height( 1 ) { } Plant( bool withLeaves, double theHeight ) : hasLeaves( withLeaves ), height (theHeight) { } void Grow() { height += 1; } void Grow( double growBy ) { height += growBy; } private: bool hasLeaves; double height; }; Now we can not only create – but also Grow a plant: Plant myPlant;// height = 1 myPlant.Grow( );// height = 2 myPlant.Grow( 3.5 );// height =5.5

Separating Interface from Implementation class Plant { public: Plant(); Plant( bool withLeaves, double theHeight ) ; void Grow(); void Grow( double growBy ); private: bool hasLeaves; double height; }; #include “Plant.h” Plant::Plant() : hasLeaves( true ), height( 1 ) { } Plant:: Plant( bool withLeaves, double theHeight ) : hasLeaves( withLeaves ), height (theHeight) { } void Plant::Grow() { height += 1; } void Plant::Grow( double growBy ) { height += growBy; } Plant.h Plant.cpp Is this the best we can do? No – short functions should be inlined!

Default parameters class Plant { public: Plant( bool withLeaves=true, double theHeight = 1) : hasLeaves( withLeaves ), height (theHeight) { } void Grow(double growBy = 1) { height += growBy ; } private: bool hasLeaves; double height; }; Same functionality: Plant myPlant;// height = 1 myPlant.Grow( );// height = 2 myPlant.Grow( 3.5 );// height = 5.5 But less code! Can we create a plant like this? Plant otherPlant(false); Plant anotherPlant(2.5);

Problem? class Plant { public: Plant( bool withLeaves=true, double theHeight = 1) : hasLeaves( withLeaves ), height (theHeight) { } void Grow(double growBy = 1) { height += growBy ; } private: bool hasLeaves; double height; }; Plant otherPlant(false); works and creates Plant without leaves and with height=1 - OK Plant otherPlant(2.5); works and creates Plant with leaves and with height=1 – Huh???

Problem? class Plant { public: Plant( bool withLeaves=true, double theHeight = 1) : hasLeaves( withLeaves ), height (theHeight) { } void Grow(double growBy = 1) { height += growBy ; } private: bool hasLeaves; double height; }; Plant otherPlant(2.5); calls Plant(bool withLeaves) after converting 2.5 to bool (to a value true) Not what we wanted! This definition is equivalent to defining 3 constructors: Plant(); Plant(bool withLeaves); Plant(bool withLeaves, double theHeight);

Solution! class Plant { public: Plant( double theHeight ) : hasLeaves( true ), height (theHeight) { } Plant( bool withLeaves=true, double theHeight = 1) : hasLeaves( withLeaves ), height (theHeight) { } void Grow(double growBy = 1) { height += growBy ; } private: bool hasLeaves; double height; }; Now Plant otherPlant(2.5); calls Plant(double theHeight) as we wanted. But how does it know what we want? And what will Plant otherPlant(1); do? We add another constructor, that gets only height as a parameter:

Overload resolution Step 1: Find candidates: Same name and viable Step 2: Find viable functions: Same number of arguments. Parameters can be converted to the types specified at function declaration. Step 3: Select best matching viable function: Determined by number of conversions needed. Several viable functions with the same number of conversions needed cause compilation error, due to ambiguity.

Overload resolution -example void f( int i ) { }; void f( double d = 4.0, int k = 1) { }; (1) f( 5.0 ); // called void f( double d = 4.0, int k = 1) (2) f( 5 ); // void f( int i ) (3) f( true); // Ambiguity error ! Both viable functions //need one conversion ! Step 1: same function name =>candidates. Step 2: viable functions. Step 3: select best matching viable. Go back to slide 14 – Plant constructor.

Construction – the details What is the difference between and Member variables are created before any class code is executed, using the values in the initialization line of the constructor If no values are given – the default construction is used: default (parameterless) constructor for classes no initialization (random value) for built-in types The first option is better – why? Plant( bool withLeaves = true, double theHeight = 1) : hasLeaves( withLeaves ), height (theHeight) { } Plant( bool withLeaves = true, double theHeight = 1) { hasLeaves = withLeaves; height = theHeight; }

Construction – the details better than It is obvious that fields are being initialized – easier to understand Better performance for members that are classes Legal even if there is no default constructor for the member Legal even if the member cannot be assigned In particular, the only way to initialize const and reference members Better suited for (multiple) inheritance (later in the course) Plant( bool withLeaves=true, double theHeight = 1) : hasLeaves( withLeaves ), height (theHeight) { } Plant( bool withLeaves=true, double theHeight = 1) { hasLeaves = withLeaves; height = theHeight; }

Array member construction Suppose we want to define a class that represents a flower clock, and contains an array of plants: class FlowerClock { public: FlowerClock() {} private: Plant plants[24]; /* Some members */ }; What happens during initialization of FlowerClock? How can we initialize all members to have height 2?

Array member construction class FlowerClock { public: FlowerClock() {} private: Plant plants[24]; /* Some members */ }; Reminder: 1. Member variables are created before any class code is executed, using the values in the initialization line of the constructor 2. If no values are given – the default construction is used to create the member 3. The default construction for arrays is: If elements are classes – initialize them using default (parameterless) constructor If elements are built-in types - no initialization (random value) So what we have is an array of 24 plants, each with values initialized by the default constructor of Plant – with leaves and with height=1

Array member construction class FlowerClock { public: FlowerClock() {} private: Plant plants[24]; /* Some members */ }; How can we initialize all plants to have height 2? For a regular array variable, we can write: Plant couple[2] = {Plant(true,2), Plant(false,2)}; For a class member that is an array – there is no way! Array members are always initialized in the default manner – no explicit values allowed! To put non-default values, we must write code inside the constructor: FlowerClock::FlowerClock() { for (size_t i=0; i<24; i++) { plants[i] = Plant(true, 2); // Assumes Plant can be assigned! }