0% found this document useful (0 votes)
50 views

Applied Programming: Engr. Abdul-Rahman Mahmood

This document provides contact information and credentials for Engr. Abdul-Rahman Mahmood, including various email addresses and online profiles. It also includes an alphanumeric code. The rest of the document discusses applying programming concepts and putting time and derived classes together through inheritance. It provides examples of a base Time class and derived ExtTime class that adds a time zone field. It discusses implementing the ExtTime class including constructors, overriding methods, and working with ExtTime objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Applied Programming: Engr. Abdul-Rahman Mahmood

This document provides contact information and credentials for Engr. Abdul-Rahman Mahmood, including various email addresses and online profiles. It also includes an alphanumeric code. The rest of the document discusses applying programming concepts and putting time and derived classes together through inheritance. It provides examples of a base Time class and derived ExtTime class that adds a time zone field. It discusses implementing the ExtTime class including constructors, overriding methods, and working with ExtTime objects.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Applied Programming

Engr. Abdul-Rahman Mahmood


DPM, MCP, QMR(ISO9001:2000)

[email protected] [email protected]
alphapeeler.sf.net/pubkeys/pkey.htm http://alphapeeler.sourceforge.net
pk.linkedin.com/in/armahmood http://alphapeeler.tumblr.com
www.twitter.com/alphapeeler [email protected]
www.facebook.com/alphapeeler [email protected]
abdulmahmood-sss alphasecure mahmood_cubix 48660186
[email protected] [email protected]
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 } ;

class ExtTime : public Time


// Time is the base class and use public inheritance
{
public :
void Set ( int h, int m, int s, ZoneType timeZone ) ;
void Write ( ) const; //overridden
ExtTime (int initH, int initM, int initS, ZoneType initZone )
;
ExtTime (); // default constructor
private :
ZoneType zone ; // added data member
};
6
Class Interface Diagram
ExtTime class

Set Set

Increment Increment Protected data:


hrs
Write Write
mins
ExtTime Time
secs
ExtTime Time
Private
data:
zone
7
Implementation of ExtTime

Default Constructor

ExtTime :: ExtTime ( ) ExtTime et1;


{
zone = EST ;
} et1
hrs = 0
The default constructor of base mins = 0
class, Time(), is automatically secs = 0
called, when an ExtTime object is
created. zone = EST

8
Implementation of ExtTime
Another Constructor

ExtTime :: ExtTime (int initH, int initM, int initS, ZoneType


initZone)
: Time (initH, initM, initS)
// constructor initializer
{
zone = initZone ;
}
ExtTime *et2 =
5000
new ExtTime(8,30,0,EST);
hrs = 8
et2
6000 mins = 30
5000
??? secs = 0
zone = EST
9
Implementation of ExtTime
void ExtTime :: Set (int h, int m, int s, ZoneType
timeZone)
{
Time :: Set (hours, minutes, seconds); // same name function
call
zone = timeZone ;
}
void ExtTime :: Write ( ) const // function overriding
{
string zoneString[8] =
{“EST”, “CST”, MST”, “PST”, “EDT”, “CDT”, “MDT”, “PDT”} ;

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

 A non-member function can access the private and


protected members of a class if it is declared a friend
of that class. That is done by including a declaration of
this external function within the class, and preceding
it with the keyword friend:
Ex03: fried class
#include <iostream>
using namespace std;
class XYZ {
private:
char ch='A';
int num = 11;
Output:
public: A
/* This statement would make class ABC 11
* a friend class of XYZ, this means that
* ABC can access the private and protected
* members of XYZ class.
*/
friend class ABC;
};
class ABC {
public:
void disp(XYZ obj){
cout<<obj.ch<<endl;
cout<<obj.num<<endl;
}
};
int main() {
ABC obj;
XYZ obj2;
obj.disp(obj2);
return 0;
}
Ex01: fried functions
// friend functions
#include <iostream>
using namespace std;

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

Rectangle duplicate (const Rectangle& param)


{
Rectangle res;
res.width = param.width*2;
res.height = param.height*2;
return res;
}

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.

 Following is a simple example of template Array class.


Ex05: Generic Class using Template
#include <iostream> template <typename T>
using namespace std;
void Array<T>::print()
template <typename T> {
class Array { for (int i = 0; i < size; i++)
private:
T* ptr;
cout << " " << *(ptr + i);
int size; cout << endl;
}
public:
Array(T arr[], int s);
void print(); int main()
}; {
int arr[5] = { 1, 2, 3, 4, 5
template <typename T>
Array<T>::Array(T arr[], int s)
};
{ Array<int> a(arr, 5);
ptr = new T[s]; a.print();
size = s; return 0;
for (int i = 0; i < size;
i++) }
ptr[i] = arr[i];
}
Output:
12345

You might also like