Professional Documents
Culture Documents
armahmood786@yahoo.com alphasecure@gmail.com
alphapeeler.sf.net/pubkeys/pkey.htm http://alphapeeler.sourceforge.net
pk.linkedin.com/in/armahmood http://alphapeeler.tumblr.com
www.twitter.com/alphapeeler armahmood786@jabber.org
www.facebook.com/alphapeeler alphapeeler@aim.com
abdulmahmood-sss alphasecure mahmood_cubix 48660186
armahmood786@hotmail.com alphapeeler@icloud.com
http://alphapeeler.sf.net/me http://alphapeeler.sf.net/acms/
Putting Them Together
Time is the base class
ExtTime is the derived class
Time
with public inheritance
The derived class can
inherit all members from the base
class, except the constructor
access all public and protected
ExtTime members of the base class
define its private data member
provide its own constructor
define its public member
functions
override functions inherited from
the base class
3
class Time Specification
// SPECIFICATION FILE ( time.h)
class Time{
public :
void Set ( int h, int m, int s ) ;
void Increment ( ) ;
void Write ( ) const ;
Time ( int initH, int initM, int initS ) ; // constructor
Time ( ) ; // default
constructor
protected :
int hrs ;
int mins ;
int secs ;
};
4
Class Interface Diagram
Time class
Set
Protected data:
Increment
hrs
Write
mins
Time secs
Time
5
Derived Class ExtTime
// SPECIFICATION FILE ( exttime.h)
#include “time.h”
enum ZoneType {EST, CST, MST, PST, EDT, CDT, MDT, PDT } ;
Set Set
Default Constructor
8
Implementation of ExtTime
Another Constructor
Time :: Write ( ) ;
cout <<‘ ‘<<zoneString[zone]<<endl;
}
10
Working with ExtTime
#include “exttime.h”
……
int main()
{
ExtTime thisTime ( 8, 35, 0, PST ) ;
ExtTime thatTime ; // default constructor
called
thatTime.Write( ) ; // outputs 00:00:00 EST
thatTime.Set (16, 49, 23, CDT) ;
thatTime.Write( ) ; // outputs 16:49:23 CDT
thisTime.Increment ( ) ;
thisTime.Increment ( ) ;
thisTime.Write ( ) ; // outputs 08:35:02 PST
}
11
Friend functions
Private & protected members of a class cannot be
accessed from outside the same class in which they are
declared.
However, this rule does not apply to "friends".
class Rectangle {
int width, height; Output:
public: 24
Rectangle() {}
Rectangle (int x, int y) : width(x), height(y) {}
int area() {return width * height;}
friend Rectangle duplicate (const Rectangle&);
};
int main () {
Rectangle foo;
Rectangle bar (2,3);
foo = duplicate (bar);
cout << foo.area() << '\n';
return 0;
}
Friend functions
Notice though that neither in the declaration of
duplicate nor in its later use in main, function
duplicate is considered a member of class Rectangle. It
isn't! It simply has access to its private and protected
members without being a member.
Ex02: friend class
#include <iostream>
using namespace std;
class Square;
class Rectangle {
int width, height;
public: Output:
int area () 16
{return (width * height);}
void convert (Square a);
};
class Square {
friend class Rectangle;
private:
int side;
public:
Square (int a) : side(a) {} //Square (int a) { side = a;}
};
void Rectangle::convert (Square a) {
width = a.side;
height = a.side;
}
int main () {
Rectangle rect;
Square sqr (4);
rect.convert(sqr);
cout << rect.area();
return 0;
}
Generics in C++
Generic Programming enables the programmer to
write a general algorithm which will work with all data
types. It eliminates the need to create different
algorithms if the data type is an integer, string or a
character.
Advantages of Generic Programming
The advantages of Generic Programming are
Code Reusability
Avoid Function Overloading
Once written it can be used for multiple times and
cases.
Templates
Generics can be implemented in C++ using Templates.
Template is a simple and yet very powerful tool in C++.
The simple idea is to pass data type as a parameter so
that we don’t need to write the same code for different
data types.
For example, a software company may need sort() for
different data types. Rather than writing and
maintaining the multiple codes, we can write one
sort() and pass data type as a parameter.
Generic Functions using Template:
We write a generic function that can be used for
different data types. Examples of function templates are
sort(), max(), min(), printArray()
Ex04: Generic Functions using Template
#include <iostream>
using namespace std;
template <typename T>
T myMax(T x, T y) Output:
{ 7
return (x > y) ? x : y; 7.5
}
g
int main()
{
// Call myMax for int
cout << myMax<int>(3, 7) << endl;
// call myMax for double
cout << myMax<double>(3.5, 7.5) << endl;
// call myMax for char
cout << myMax<char>('g', 'e') << endl;
return 0;
}
Generic Class using Template:
Like function templates, class templates are useful
when a class defines something that is independent of
data type. Can be useful for classes like LinkedList,
binary tree, Stack, Queue, Array, etc.