Download presentation
Presentation is loading. Please wait.
1
Static Class Member Function We can declare a member function being static. A static member function doesn’t have to be invoked by an object. A static member function can use only static data member.
2
Returning Objects Return a reference to an object A constant reference to an object An object A constant object
3
A queue holds an ordered sequences of items. Has a limit Add item to the end of the queue Remove item from front of the queue. Create an empty queue Check whether queue is empty Check whether queue is full Example(Queue simulation)
4
#ifndef Queue_H #define Queue_H class Queue { private : enum {Q_SIZE=10;} struct Node {int item; struct Node *next; }; Node *front; Node *end; int items; const int qsize;
5
public : Queue( int qs =Q_SIZE ); // creates queue with a qs limit ~Queue(); bool isempty(); bool isfull(); int queuecount(); bool enqueue( const int &item); bool dequeue(int &item); int get_size(); };
6
Queue :: Queue(int qs) { front=end =NULL; item=0; qsize=qs; // not acceptable; since qsize is // constant } What to do ?
7
Using syntax member initialize list Queue :: Queue(int qs) : qsize(qs) // initialize //qsize to qs { front=end =NULL; item=0; } Or Queue :: Queue(int qs) : qsize(qs), front(NULL), end(NULL), items(0) { }
8
This form can be used with constructors We have to use this form class member that are declared as reference. e.g. class Agency {…}; class Agent { private : Agency & belong; }; Agent::Agent (Agency & a) : belong(a) {…}
9
bool Queue :: enqueue(const int &item) { if( isfull()) return false; Node * add=new Node; If( add==NULL) return false; add->item=item; add->next=NULL; items++; if( front == NULL) front=add; else end ->next=add; end=add; return true; }
10
bool Queue :: dequeue(int &item) { if( front ==end) return false; item=front->item; items--; Node *temp=front; front=front->next; delete temp; if (items==0) end=NULL; return true; }
11
bool Queue :: isempty() { if (item==0) return true; else return false; } bool Queue :: isfull() { if (item==qsize) return true; else return false; }
12
Queue :: ~ Queue () { Node *temp; while( front !=NULL) { temp=front; front=front->next; delete temp; }
13
int Queue :: get_size() { return qsize; }
14
#include #include “queue.h” int main() { Queue qu; Queue qu1(12); cout<<qu.get_size()<<endl; cout<<qu1.get_size()<<endl; return 0; }
15
Class Inheritance The goal of OOP is providing reusable code. Traditional C provides reusability through predefined, precompiled function e.g strln(), rand();
16
Often, class libraries are available in source code, which means we can modify them. C++ has better method than code modification. It’s called inheritance. Deriving new class from old one( base class) We can do the following with inheritance:
17
Add functionality to an existing class. e.g. Given a basic array class we can add arithmetic operations. Add to the data that a class represent. derive a class from class string having a color element to show the color of the string Modify class method behavior.
18
The ability to define the behavior or implementation of one class as a superset of another class
19
#ifndef TABTENN0_H_ #define TABTENN0_H_ class TableTennisPlayer { private : enum{LIM=20}; char firstname[LIM]; char lastname[LIM]; bool hasTable; public : TableTennisPlayer ( const char *fn=“none”, const char *In =“none”, bool ht=false); void name (); bool HasTable() { return hasTable;}; void ResetTable(bool v) { hasTable=v;}; }; #endif
20
#include “tabtenn0.h” #include TableTennisPlayer:: TableTennisPlayer( const char *fn, const char *ln, bool ht) { strncpy(firstname, fn, LIM-1); firstname[LIM-1]=‘\0’; strncpy(lastname, ln, LIM-1); lastname[LIM-1]=‘\0’; hasTable=ht; } void TableTennisPlayer::name() { cout<<lastname<<“, “<<firstname; }
21
The following class is a derived class and manage the points that player earned. class RatedPlayer : public TableTennisPlayer { … }; The colon indicates that RatedPlayer class is based on the TableTennisPlayer.
22
If we declare a RatedPlayer object it has the following special properties. An object of the derived type stores the data members of the base type in it. An object of the derived type can use the method of the base type. A derived class needs its own constructers A derived class can add additional data members.
23
class RatedPlayer : public TableTennisPlayer { private : unsigned int rating; public : RatedPlayer ( unsigned int r =0, const char *fn=“none”, const char *In =“none”, bool ht=false); RatedPlayer ( unsigned int r, const TableTennisPlayer &tp); unsigned int Rating() { return rating;} void ResetRating ( unsigned int r) { rating = r;} };
24
A derived class does not have direct access to the private member of the base class. They have to use public base-class methods to access private base-class member. When a program construct a derived-class object, it first constructs the base-class object.
25
We can use C++ member initializer list syntax: RatedPlayer:: RatedPlayer( unsigned int r, const char *fn, const char *In, bool ht) : TableTennisPlayer(fn, ln, ht) { rating=r; } Or we can write : RatedPlayer:: RatedPlayer( unsigned int r, const TableTennisPlayer &tp) : TableTennisPlayer(tp) { rating=r; } Or RatedPlayer:: RatedPlayer( unsigned int r, const TableTennisPlayer &tp) : TableTennisPlayer(tp), rating(r); { }
26
We can call RatedPlayer rplayer1(1140,”Mary”, “Smith”,true);
27
#ifndef TABTENN0_H_ #define TABTENN0_H_ class TableTennisPlayer { private : enum{LIM=20}; char firstname[LIM]; char lastname[LIM]; bool hasTable; public : TableTennisPlayer ( const char *fn=“none”, const char *In =“none”, bool ht=false); void name (); bool HasTable() { return hasTable;}; void ResetTable(bool v) { hasTable=v;}; };
28
class RatedPlayer : public TableTennisPlayer { private : unsigned int rating; public : RatedPlayer ( unsigned int r =0, const char *fn=“none”, const char *In =“none”, bool ht=false); RatedPlayer ( unsigned int r, const TableTennisPlayer &tp); unsigned int Rating() { return rating;} void ResetRating ( unsigned int r) { rating = r;} }; #endif
29
#include “tabtenn0.h” #include TableTennisPlayer:: TableTennisPlayer( const char *fn, const char *ln, bool ht) { strncpy(firstname, fn, LIM-1); firstname[LIM-1]=‘\0’; strncpy(lastname, ln, LIM-1); lastname[LIM-1]=‘\0’; hasTable=ht; } TableTennisPlayer::name() { cout<<lastname<<“, “<<firstname; }
30
RatedPlayer:: RatedPlayer( unsigned int r, const char *fn, const char *In, bool ht) : TableTennisPlayer(fn, ln, ht) { rating=r; } RatedPlayer:: RatedPlayer( unsigned int r, const TableTennisPlayer &tp) : TableTennisPlayer(tp) { rating=r; }
31
#include #include “tabtenn1.h” int main() { TableTennisPlayer player1(“tara”,”Smith”,false); RatedPlayer rplayer1(1140,”Dave”,”Cohen”,true); rplayer1.name(); if(rplayer1.HasTable()) cout<<“has a table \n”; else cout<<“hasn’t a table\n”; player1.name();
32
if(player1.HasTable()) cout<<“has a table \n”; else cout<<“hasn’t a table\n”; rplayer1.name(); cout<<“rating “<< rplayer1.Rating()<<endl; RatedPlayer rplayer2(1212,player1); rplayer2.name(); cout<<“rating “<< rplayer2.Rating()<<endl; return 0; }
33
Relation Between Derived-Class and Base-Class Derived class object can use base class methods, if they are not private. Base class pointer can point to a derived class object A base class reference can refer to a derived class object A derived class reference or pointer can NOT refer to base class object.
34
RatedPlayer rplay(10,”Sue”,”Smith”,true); TableTennisPlayer &rt= rplay; TableTennisPlayer *pt= &rplay; rt.name(); // call by references pt->name(); //call by pointer TableTennisPlayer play(“dan”, “cohen”,true); RatedPlayer &rr=play; // not allowed RatedPlayer *pr=&play; // not allowed
35
TableTennisPlayer *ptr; ptr = &rplayer2; ptr->name(); cout<<endl; cout Rating()<<endl; //not ok
36
Derived class inherits the base class methods and data member, so derived class method can use base class reference to call a method. A base class can’t use derived class method therefore base class method can not use derived class reference to call a method.
37
Three types of inheritance : Public Private Protected Public Inheritance is an Is-a relation. An object of derived class is an object of base class. Consider Fruit and Banana, Banana is a Fruit.
38
public inheritance doesn’t model a has-a relationship. Consider Lunch and Fruit. In general fruit is not lunch, but lunch can have fruit. public inheritance doesn’t model a is-like-a relationship. Lawyers are like sharks, (some times) Don’t try to derived a Lawyer class from Shark. Otherwise lawyers must live underwater!
39
public inheritance doesn’t model an is- implemented-as-a relationship. Stack and Array. Not proper to derive a Stack from Array class public inheritance doesn’t model a uses-a relationship. Computer can use printer, but we don’t derived printer class from computer class.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.