inline functions, operator overloading and inheritance cosc 102: programming in c++

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

Upload: bartholomew-robinson

Post on 26-Dec-2015

239 views

Category:

Documents


1 download

TRANSCRIPT

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

Inline Functions, Operator Overloading and Inheritance

COSC 102: Programming in C++

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

2

Outline

• Announcements– Cheating

• Review of Assignment• Inline• Operator Overloading• Lab• Inheritance

OLA/COSC 102

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

3

Review of Last Week’s Assignment

• Two weeks ago assignment– bool isLeapYear();– int calcDayOfYear();

• Add it to the Date class

OLA/COSC 102

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

4

bool isLeapYear(){if(year%4 == 0 && year % 100 != 0)

return true; else if(year % 400 == 0)

return true; elsereturn false;

}Or{if((year%4 == 0 && year % 100 != 0) || (year %400 ==0)) return true;else return false;}

OLA/COSC 102

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

5

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 102

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

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

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

7

Inline Declaration Exampleclass 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

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

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 inlineinline int Date::getYear(){

return year;}

OLA/COSC 102

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

9

Inline Declarations?

Short functions are good candidates for inline functions, but long functions are not.

Space

OLA/COSC 102

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

10

Operator Overloading

• Comparing objects == < >• Reading in objects >>• Displaying objects <<

OLA/COSC 102

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

11

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 102

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

12

• 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 102

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

13

• 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 102

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

14

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 102

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

15

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 102

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

16

Questions

OLA/COSC 102

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

17

Human Class Lab

• string name• string color• float size;

OLA/COSC 102

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

18

Assignment on Fruit Class

OLA/COSC 102

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

Inheritance

COSC 102: Programming in C++

Page 20: Inline Functions, Operator Overloading and 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 102 20

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

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 102 21

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

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 102 22

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

• 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 102 23

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

#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 102 24

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

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 102 25

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

Void NaijaDate:: printNDate(){

printDate();cout<<“This is a naija Date”<<endl;cout<<“The time is ”<<time<<endl;

}

OLA/COSC 102 26

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

Main.cpp

#include “NaijaDate.h”Int main(){

NaijaDate nd;nd.printDate();nd.getYear();nd.printNDate();

}OLA/COSC 102 27

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

• 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 102 28

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

Fruit and Banana Class

OLA/COSC 102 29

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

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 102 30

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

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

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

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 102 32

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

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 102 33

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

• 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 102 34

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

• 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 102 35

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

• 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 102 36

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

OLA/COSC 102 37

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

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 102 38

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

class A class B{ {// base class // 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 102 39

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

• 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 102 40