Download presentation
Presentation is loading. Please wait.
Published byBartholomew Robinson Modified over 9 years ago
1
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 1022
3
Review of Last Week’s Assignment Two weeks ago assignment – bool isLeapYear(); – int calcDayOfYear(); Add it to the Date class OLA/COSC 1023
4
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
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 1025
6
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
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
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
9 Inline Declarations? Short functions are good candidates for inline functions, but long functions are not. Space OLA/COSC 102
10
Operator Overloading Comparing objects == Reading in objects >> Displaying objects << OLA/COSC 10210
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 10211
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 10212
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 10213
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 10214
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 10215
16
Questions OLA/COSC 10216
17
Human Class Lab string name string color float size; OLA/COSC 10217
18
Assignment on Fruit Class OLA/COSC 10218
19
Inheritance COSC 102: Programming in C++
20
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
21
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
22
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
23
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
24
#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
25
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
26
Void NaijaDate:: printNDate() { printDate(); cout<<“This is a naija Date”<<endl; cout<<“The time is ”<<time<<endl; } OLA/COSC 10226
27
Main.cpp #include “NaijaDate.h” Int main() { NaijaDate nd; nd.printDate(); nd.getYear(); nd.printNDate(); } OLA/COSC 10227
28
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
29
Fruit and Banana Class OLA/COSC 10229
30
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
31
The table summarizes the three access specifiers in C++. OLA/COSC 10231
32
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
33
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
34
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
35
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
36
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
37
OLA/COSC 10237
38
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
39
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
40
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.