Inline Functions, Operator Overloading and Inheritance COSC 102: Programming in C++

Slides:



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

INHERITANCE BASICS Reusability is achieved by INHERITANCE
CSE 1302 Lecture 8 Inheritance Richard Gesick Figures from Deitel, “Visual C#”, Pearson.
You gotta be cool. Inheritance Base Classes and Derived Classes Inheritance: Public, Protected, Private What is inherited from the base class? Multiple.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Inheritance1 Inheritance Software re-use with derived classes.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
2 Objectives You should be able to describe: Operator Functions Two Useful Alternatives – operator() and operator[] Data Type Conversions Class Inheritance.
1 Fall 2007ACS-1903 Chapter 6: Classes Classes and Objects Instance Fields and Methods Constructors Overloading of Methods and Constructors Scope of Instance.
Chapter Objectives You should be able to describe: Object-Based Programming Classes Constructors Examples Common Programming Errors.
 2006 Pearson Education, Inc. All rights reserved. Templates (again)CS-2303, C-Term Templates (again) CS-2303 System Programming Concepts (Slides.
Inheritance, Polymorphism, and Virtual Functions
Chapter 12: Adding Functionality to Your Classes.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 4 Objects and Classes.
Lecture 8 Inheritance Richard Gesick. 2 OBJECTIVES How inheritance promotes software reusability. The concepts of base classes and derived classes. To.
Chapter 8 More Object Concepts
1 Understanding Inheritance COSC 156 C++ Programming Lecture 8.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
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.
The Java Programming Language
CS212: Object Oriented Analysis and Design Lecture 15: Inheritance in C++ -II.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 13 Introduction to Classes.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
JavaScript Syntax and Semantics. Slide 2 Lecture Overview Core JavaScript Syntax (I will not review every nuance of the language)
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
Java™ How to Program, 10/e © Copyright by Pearson Education, Inc. All Rights Reserved.
At the end of the module the students should be able to:  Familiarize themselves with the different uses of constants, operators, and expressions in.
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.
C++ Programming Basic Learning Prepared By The Smartpath Information systems
Copyright 2006 Oxford Consulting, Ltd1 February Polymorphism Polymorphism Polymorphism is a major strength of an object centered paradigm Same.
Chapter 7 Understanding Inheritance. LOGO Objectives  Learn about inheritance and its benefits  Create a derived class  Learn about restrictions imposed.
Copyright © 2012 Pearson Education, Inc. Chapter 15: Inheritance, Polymorphism, and Virtual Functions.
Chapter 4 Introduction to Classes, Objects, Methods and strings
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Introduction to c++ programming - object oriented programming concepts - Structured Vs OOP. Classes and objects - class definition - Objects - class scope.
EGR 2261 Unit 11 Classes and Data Abstraction  Read Malik, Chapter 10.  Homework #11 and Lab #11 due next week.  Quiz next week.
By Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 13 More on Classes Chapter 8 Week 13 More on Classes Chapter 8.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Objects and Classes.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Classes, Interfaces and Packages
Lecture 2 Functions. Functions in C++ long factorial(int n) The return type is long. That means the function will return a long integer to the calling.
Object-Oriented Programming: Inheritance and Polymorphism.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
Polymorphism & Virtual Functions 1. Objectives 2  Polymorphism in C++  Pointers to derived classes  Important point on inheritance  Introduction to.
Defining Data Types in C++ Part 2: classes. Quick review of OOP Object: combination of: –data structures (describe object attributes) –functions (describe.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X 1 Chapter 9 Introduction of Object Oriented Programming.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Data Structures Lecture 4: Classes in C++ Azhar Maqsood NUST Institute of Information Technology (NIIT)
Computer Programming II Lecture 5. Introduction to Object Oriented Programming (OOP) - There are two common programming methods : procedural programming.
EGR 2261 Unit 13 Classes Read Malik, Chapter 10.
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.
Andy Wang Object Oriented Programming in C++ COP 3330
Object Oriented Programming COP3330 / CGS5409
JavaScript Syntax and Semantics
Operator Overloading BCA Sem III K.I.R.A.S.
Understanding Inheritance
Chapter 1: Computer Systems
Chapter 9 Objects and Classes
Dr. Bhargavi Dept of CS CHRIST
Object-Oriented Programming: Inheritance and Polymorphism
Operator Overloading Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Fundaments of Game Design
COP 3330 Object-oriented Programming in C++
CMSC202 Computer Science II for Majors Lecture 07 – Classes and Objects (Continued) Dr. Katherine Gibson Based on slides by Chris Marron at UMBC.
Chap 2. Identifiers, Keywords, and Types
Presentation transcript:

Inline Functions, Operator Overloading and Inheritance COSC 102: Programming in C++

Outline Announcements – Cheating  Review of Assignment Inline Operator Overloading Lab Inheritance OLA/COSC 1022

Review of Last Week’s Assignment Two weeks ago assignment – bool isLeapYear(); – int calcDayOfYear(); Add it to the Date class OLA/COSC 1023

bool isLeapYear() { if(year%4 == 0 && year % 100 != 0) return true; else if(year % 400 == 0) return true; else return false; } Or { if((year%4 == 0 && year % 100 != 0) || (year %400 ==0)) return true; else return false; } OLA/COSC 1024

int CalcDayOfYear() { int dayCount[12] = {31,28,31,30,31,30, 31,31,30,31,30,31}; int dayOfYear = 0; for(int i = 1; i < month; ++i) { dayOfYear += dayCount[i-1]; if(i == 2 && isLeapYear() == true) dayOfYear += 1; } dayOfYear += day; return dayOfYear; } OR USE SWITCH STATEMENT OLA/COSC 1025

6 Inline Declaration Inline functions play an important role in class declarations. When a function is implemented inside a class declaration, it automatically becomes an inline function. This is also known as inline declaration. Why Speed: function calls are expensive OLA/COSC 102

7 Inline Declaration Example class Date { private: int year; int day; int month; public: Date(); int getMonth(); int getYear () { return year; } void setMonth(int m) { month = m;} bool isLeapYear(); }; OLA/COSC 102

8 Inline Functions in Implementation File There is another way to declare inline functions for classes. You may declare inline functions in the class’s implementation file. For example, to declare function f2 as an inline function, precede the inline keyword in the function header as follows: // Implement function as inline inline int Date::getYear() { return year; } OLA/COSC 102

9 Inline Declarations? Short functions are good candidates for inline functions, but long functions are not. Space  OLA/COSC 102

Operator Overloading Comparing objects == Reading in objects >> Displaying objects << OLA/COSC 10210

Overloaded Operators and Objects The C++ language provides many symbol operators. The C++ operators are designed for specific tasks and data types. There are: – relational (, =,==, !=) – logical (&&, ||, !) – arithmetic (+,–, *, /, %) – increment and decrement (++, – –) – modulus (%) OLA/COSC 10211

C++ operators (which are just symbols) perform different tasks, depending on how they are used. To overload an operator, we must write a specific operator function that is a class member. This operator function defines a valid C++ operator that performs the tasks when it is working with an object of that class. You may use only valid C++ operators as overloaded operators in classes. Note: You cannot use characters that are not already operators in C++. OLA/COSC 10212

The prototype format for an overloaded operator function contains the keyword operator and is followed by the desired operator symbol: return_type operator symbol(input parameter list); bool operator == (const Date& d); The overloaded operators are just regular class functions and follow the same rules. When you overload your operators, you have to determine what characteristics makes one object comparable to another object. OLA/COSC 10213

bool Date::operator == (const Date& d) { if (year != d.year) return false; if(month != d.month) return false; if(day != d.day) return false; return true; } bool Date::operator == (const Date& d) { if((year==d.year) &&(month ==d.month) &&(day ==d.day)) return true; else return false; } OLA/COSC 10214

Main.cpp int main() { Date tobiFemale(1,2,1993); Date mayowaYellow(2,1,1993); if (tobiFemale==mayowaYellow) cout<<“They are twins”<<endl; else cout<<“Sowwy”<<endl; } OLA/COSC 10215

Questions OLA/COSC 10216

Human Class Lab string name string color float size; OLA/COSC 10217

Assignment on Fruit Class OLA/COSC 10218

Inheritance COSC 102: Programming in C++

Parents and Children Imagine having the ability to create a class (a parent class), then derive a new class (a child class) from the parent. This child class has the properties and characteristics of the parent. The programmer can add additional components unique to the child class. OLA/COSC 10220

Why Is Inheritance So Important? One reason C++ programmers like the language is because of the many pre-built classes ( vector, string, queue, etc.) Many C++ classes have been built, and are intended to be starting points for developers, so that the developer can customize the class as needed. REUSABILITY OLA/COSC 10221

Inheritance Basics The class relationship that models inheritance is the“is a” relationship. The base class, typically, is a general-purpose class. The derived class is a special case of the base class. The phrase “is a” describes how the classes are related. OLA/COSC 10222

The format for class inheritance: class BaseClass { // members of the base class }; class DerivedClass : access_specifier BaseClass { // members of the derived class inherit // protected and public members of the // base class }; class CisStudent: public Student { }; OLA/COSC 10223

#include “Date.h” class NaijaDate : public Date { private: string time; public: NaijaDate(); NaijaDate(int d, int m, int y, string des, string t); }; OLA/COSC 10224

NaijaDate.cpp NaijaDate::NaijaDate() { time = “NaijaTime”; } NaijaDate::NaijaDate(int d, int m, int y, string des, string t): Date(d,m,y,des) { time = t; } OLA/COSC 10225

Void NaijaDate:: printNDate() { printDate(); cout<<“This is a naija Date”<<endl; cout<<“The time is ”<<time<<endl; } OLA/COSC 10226

Main.cpp #include “NaijaDate.h” Int main() { NaijaDate nd; nd.printDate(); nd.getYear(); nd.printNDate(); } OLA/COSC 10227

This line contains a colon (:) which is C++’s way of saying this BaseClass is the parent for the new DerivedClass. class DerivedClass : access_specifier BaseClass The access specifier, usually the public specifier, dictates access properties for the inherited members. Once you have built the new, derived class, you can create derived class objects and use them in the normal fashion. OLA/COSC 10228

Fruit and Banana Class OLA/COSC 10229

Protected Members There is a third specifier in C++ designed just for inheritance. The protected access specifier provides the programmer greater flexibility when he is working with inherited classes. When a class member is specified as protected, that member is inherited by the derived classes, but the member is not accessible outside the base, or derived class. OLA/COSC 10230

The table summarizes the three access specifiers in C++. OLA/COSC 10231

Access Specifier Specifics and Multiple Inheritance There are three access specifiers for classes: private, protected, and public. We are familiar with the role these three play when they are used inside a class declaration, such as: OLA/COSC 10232

class C { private: // members accessible only to class members protected: // members that are inherited by the child // class // treated as private to the world public: // members that are inherited by child //class are accessible to the world via an //object of class C }; OLA/COSC 10233

Another place we use these specifiers is in the first line of a class declaration. When used in the first line of a declaration, they are known as base class access specifiers. The format is shown below: class A : public B { // class info here }; OLA/COSC 10234

The “ public B ” is the base class access specifier. It is possible to create derived classes by using all three of the specifiers: public, protected, and private. This base specifier dictates how the base members are treated in the derived classes. OLA/COSC 10235

The following table summarizes the base class specifiers if class A (below) is used as a base class. class A { private: // private members protected: // protected members public: // public members }; OLA/COSC 10236

OLA/COSC 10237

Multiple Inheritance It is possible to derive a new class from two or more base classes. It is possible to have multiple parents for a child class. The syntax for this type of inheritance is shown on the next slide: OLA/COSC 10238

class Aclass B{// base class}; class C : public A, public B { // derived class from two base //classes // class C has both public and //protected members from A and B }; OLA/COSC 10239

The derived class must have the parent classes in a comma-separated list, and base access specifiers are needed. Be sure that the new class does indeed have an is a relationship with both base classes. Beginning C++ programmers may believe that using a multiple-base inherited derived class is a time-saving solution for their program. In truth, multiple derived classes pose rather complicated design and implementation issues. OLA/COSC 10240