static class member function we can declare a member function being static. a static member function...

39
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.

Post on 22-Dec-2015

230 views

Category:

Documents


1 download

TRANSCRIPT

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.

Returning Objects

• Return a reference to an object

• A constant reference to an object

• An object

• A constant object

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

#ifndef Queue_H#define Queue_Hclass Queue {private : enum {Q_SIZE=10;} struct Node {int item; struct Node *next; }; Node *front; Node *end; int items; const int qsize;

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();};

Queue :: Queue(int qs)

{

front=end =NULL;

item=0;

qsize=qs; // not acceptable; since qsize is

// constant

}

What to do ?

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){}

• 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) {…}

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;}

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;}

bool Queue :: isempty() { if (item==0) return true; else return false; } bool Queue :: isfull() { if (item==qsize) return true; else return false; }

Queue :: ~ Queue ()

{

Node *temp;

while( front !=NULL)

{

temp=front;

front=front->next;

delete temp;

}

}

int Queue :: get_size()

{

return qsize;

}

#include<iostream.h>

#include “queue.h”

int main() {

Queue qu;

Queue qu1(12);

cout<<qu.get_size()<<endl;

cout<<qu1.get_size()<<endl;

return 0;

}

Class Inheritance

The goal of OOP is providing reusable code.

Traditional C provides reusability through predefined, precompiled function

e.g

strln(), rand();

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:

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.

The ability to define the behavior or implementation of one class as a superset of another class

#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

#include “tabtenn0.h”#include <iostream.h>#include<cstring>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; }

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.

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.

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;}};

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.

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);{ }

We can call

RatedPlayer rplayer1(1140,”Mary”, “Smith”,true);

#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;};

};

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

#include “tabtenn0.h”#include <iostream.h>#include<cstring>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; }

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; }

#include<iostream.h>

#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();

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;

}

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.

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

TableTennisPlayer *ptr;

ptr = &rplayer2;

ptr->name();

cout<<endl;

cout<<ptr->Rating()<<endl; //not ok

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.

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.

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!

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.