Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 40

SFG

Sal F. Gambino
CS-342

C++ CS 342

Data Structures
&
OOD 1
SFG
C++ Sal F. Gambino
CS-342

Procedural Object
programming Oriented Design
languages languages

•the unit of programming •the unit of programming


is a FUNCTION is a CLASS
•action -oriented •object are instantiated (created)
•operations
member functions
•data members
2
SFG
Accessible Class example Sal F. Gambino
to all CS-342
elements
Public Private NOT
data data accessible
outside the
Public functions class
boundary
Private functions

Classes encapsulate data and functions together


3
SFG
Implementing a class Sal F. Gambino
CS-342
class time constructor
{public: time(); Member
void setTime(int, int, int); functions
void printMilitaryTime();
interface void printStandard();

private: int hour; Data


int min; members
int sec;
};
4
SFG
Sal F. Gambino
Constructor CS-342

• A member function with the same name as the class


• initializes the data members of a class object
• is called automatically when an object of that class
is created or instantiated
• cannot specify return types or return values
• can contain default arguments

5
Example of using a constructor SFG
class Salesperson Sal F. Gambino
{public: Salesperson(); The constructor
CS-342
-- --- --- -- -- Salesperson
private: double sales[12];
is called and the
---- --- -- ---
data member sales
is initialized
Implementation Salesperson::Salesperson()
{
for( int c =0; c <= 12; c++)
sales[c]=0.0;
}
Object s is
Instantiated: Salesperson s; instantiated 6
SFG
Example of using a constructor
class Time Sal F. Gambino
{public: Time(); CS-342
void setTime(int, int, int);
-- --- --- -- -- The constructor Time
private: int hr;
int min;
is called and
int sec; the data members
are initialized

Implementation Time::Time() { hr = min = sec = 0 ;}

Instantiated: Time t; Object t is


instantiated
7
default arguments & constructors SFG
class Time
Sal F. Gambino
{public: Time( int =0, int=0, int=0); Default values of
CS-342
void setTime(int, int, int); zeros
-- --- --- -- --
private: int hr; int min; int sec; 0 0 0
Implementation Time::Time(int hr, int min, int sec)
{ setTime( hr, min, sec ); }

hr =0 void Time::setTime(int h, int m, int s )


{ hr = ( h >= 0 && h < 24 ) ? h : 0;
min =0
min = ( m >= 0 && m < 60 ) ? m : 0;
sec =0 sec = ( s >= 0 && s < 60 ) ? s : 0;
}
No arguments
Instantiated: Time t1; passed 8
default arguments & constructors SFG
class Time 2 Sal F. Gambino
{public: Time( int =0, int=0, int=0); CS-342
void setTime(int, int, int);
-- --- --- -- --
private: int hr; int min; int sec; 2 0 0
Implementation Time::Time(int hr, int min, int sec )
{ setTime( hr, min, sec ); }

hr =2 void Time::setTime(int h, int m, int s )


{ hr = ( h >= 0 && h < 24 ) ? h : 0;
min =0
min = ( m >= 0 && m < 60 ) ? m : 0;
sec =0 sec = ( s >= 0 && s < 60 ) ? s : 0;
}

Instantiated: Time t2(2);


9
default arguments & constructors SFG
class Time 21 34 Sal F. Gambino
{public: Time( int =0, int=0, int=0); CS-342
void setTime(int, int, int);
-- --- --- -- --
private: int hr; int min; int sec; 21 34 0
Implementation Time::Time(int hr, int min, int sec )
{ setTime( hr, min, sec ); }

hr = 21 void Time::setTime(int h, int m, int s )


{ hr = ( h >= 0 && h < 24 ) ? h : 0;
min = 34
min = ( m >= 0 && m < 60 ) ? m : 0;
sec = 0 sec = ( s >= 0 && s < 60 ) ? s : 0;
}

Instantiated: Time t3( 21, 34 );


10
arguments & constructors SFG
class Time 25 14 42 Sal F. Gambino
{public: Time( int =0, int=0, int=0); CS-342
void setTime(int, int, int);
-- --- --- -- --
private: int hr; int min; int sec; 25 14 42
Implementation Time::Time(int hr, int min, int sec )
{ setTime( hr, min, sec ); }

hr = 0 void Time::setTime(int h, int m, int s )


{ hr = ( h >= 0 && h < 24 ) ? h : 0;
min = 14
min = ( m >= 0 && m < 60 ) ? m : 0;
sec = 42 sec = ( s >= 0 && s < 60 ) ? s : 0;
}

Instantiated: Time t4( 25, 14, 42 );


11
arguments & constructors SFG
class Time 27 74 99 Sal F. Gambino
{public: Time( int =0, int=0, int=0); CS-342
void setTime(int, int, int);
-- --- --- -- --
private: int hr; int min; int sec; 27 74 99
Implementation Time::Time(int hr, int min, int sec )
{ setTime( hr, min, sec ); }

hr = 0 void Time::setTime(int h, int m, int s )


{ hr = ( h >= 0 && h < 24 ) ? h : 0;
min = 0
min = ( m >= 0 && m < 60 ) ? m : 0;
sec = 0 sec = ( s >= 0 && s < 60 ) ? s : 0;
}

Instantiated: Time t5( 27, 74, 99 );


12
A simple class
SFG
Sal F. Gambino
The following example shows the syntax for CS-342
specifying a two-dimensional vector class
vector. A two dim vector has a pair of
coordinates(x,y), where x and y are some real
numbers. The addition of two vectors
v1=( x1, y1 ) and v2 = ( x2, y2 ) is defined as
v1 + v2 = ( x1 + x2, y1 + y2 ) = v3.
Ex: if v1 = ( 3.0 , 2.0 )
v2 = ( 3.5 , 4.5 )

then v3 = ( 3.0 + 3.5 , 2.0 + 4.5 )


or v3 = ( 6.5 , 6.5 )
13
A simple class
SFG
Sal F. Gambino
Operations of class vector: CS-342
•constructor with NO arguments
•constructor/s with arguments
•set data
•print data
•add vectors

Data of class vector:


•x coordinate
•y coordinate

14
SFG
Sal F. Gambino
CS-342
class vector
{ public: vector()
vector(float x, float y );
void setdata();
void printdata();
void add_vectors();
private: float xCo, yCo;
}

15
// vecthead.h
Vector header
SFG
// --------- doc. For each member functions ------ Sal F. Gambino
// ---------- pre or post etc. CS-342
#ifndef vecthead_h
#define vecthead_h
class vector
{ public: vector();
vector(float , float );
void setdata();
void printdata();
void add_vector(vector ,vector);
private: float xCo, yCo;
};
#endif 16
Vector implementation
SFG
#include <iostream.h>
#include <conio.h> Sal F. Gambino
#include "vecthead.h" CS-342

vector::vector() { }
vector::vector(float x, float y)
{ xCo = x;
yCo = y;
}
void vector::setdata()
{
cout << "\n Enter X-Coord.= "; cin >> xCo;
cout << "\n Enter Y-Coord.= "; cin >> yCo;
} 17
Vector implementation SFG
void vector::printdata()
Sal F. Gambino
{
CS-342
cout << "\n (X,Y) = ”
<< "("
<< xCo
<< ","
<< yCo << ")";
}
void vector::add_vector(vector a, vector b)
{
xCo = a.xCo + b.xCo;
yCo = a.yCo + b.yCo;
}
18
#include <iostream.h> Vector driver SFG
#include <conio.h>
Sal F. Gambino
#include "vecthead.h"
CS-342
// -------------------driver of class vector
void main()
{ vector v1, v3; // objects v1,v3 are defined
vector v2( 3.5, 4.5 ); // object v2 is instantiated
v1.setdata(); // calls member function
v1.printdata(); // to get data
v2.printdata();

v3.add_vector(v1,v2); // add v1 and v2 giving


v3.printdata(); // vectors = v3
getch();
19
}
Vector implementation Main()
SFG
*vector v1, v2;
#include <iostream.h>
*vector v2( 3.5, 4.5 );
#include <conio.h> Sal F. Gambino
v1.setdata();
#include "vecthead.h" v1.printdata(); CS-342
v2.printdata();
vector::vector() { } v3.add_vector(v1,v2);
3.5 4.5 v3.printdata();
vector::vector(float x, float y)
{ xCo = x; yCo = y; } v2.xCo = 3.5
v2.yCo = 4.5
void vector::setdata()
{ cout << "\n Enter X-Coord.= "; cin >> xCo;
cout << "\n Enter Y-Coord.= "; cin >> yCo;
Object v1,v3
Object v2 is vector v1, v3;
are
instantiated vector v2( 3.5, 4.5 ); defined
20
SFG
Vector implementation Main() *v1.setdata();
*v1.printdata();
#include <iostream.h> *v2.printdata();
#include <conio.h> v3.add_vector(v1,v2); Sal F. Gambino
#include "vecthead.h" v3.printdata(); v1.xCo =3
CS-342

void vector::setdata() v1.yCo = 2


{ cout << "\n Enter X-Coord.= "; cin >> xCo;
cout << "\n Enter Y-Coord.= "; cin >> yCo;
}
void vector::printdata()
{ cout << "\n (X,Y) = " << "(" << xCo << "," << yCo << ")"; }
v2.xCo = 3.5 Call:
v1.setdata(); Output:
v2.yCo = 4.5 Call: (X,Y) = ( 3.0,2.0 )
v1.xCo = 3.0 v1.printdata();
Call:
(X,Y) = ( 3.5,4.5 )
21
v1.yCo = 2.0 v2.printdata();
Vector implementation Call:
v3.printdata();
SFG
#include <iostream.h>
Main() ------
#include <conio.h> Sal F. Gambino
*v3.add_vector(v1,v2); Output: (X,Y) = (3.0,2.0)
#include "vecthead.h" CS-342
*v3.printdata(); (X,Y) = (3.5,4.5)
void vector::printdata() (X,Y) = (6.5, 6.5)
{ cout << "\n (X,Y) = " << "(" << xCo << "," << yCo << ")";
v3 v1(3.0, 2.0) v2(3.5, 4.5)

void vector::add_vector( vector a, vector b)


{ xCo = a.xCo + b.xCo;
yCo = a.yCo + b.yCo; v3.xCo = 3.0 + 3.5 = 6.5
} v3.yCo = 2.0 + 4.5 = 6.5

v1.xCo = 3.0 v2.xCo = 3.5 Call:


v3.add_vector(v1,v2);
v1.yCo = 2.0 v2.yCo = 4.5 22
Rectangle problem
SFG
Sal F. Gambino
Create a Class rectangle. The class hasattributes CS-342
LENGTH and WIDTH, each of which defaults to 1.

It has member functions that calculate the PERIMETER


and the AREA of the rectangle and a PRINTALL function
to display the length, width, area, and perimeter.

It has SET and GET functions for both LENGTH and


WIDTH. The set functions should VERIFY that length and
width are each floating point numbers larger than 0.0 and
less than 20.0 Create a project file, compile and execute.
23
A simple class
SFG
Sal F. Gambino
Operations of class rectangle: CS-342
•constructor with NO arguments Input: l,w
•constructor/s with arguments output: a,p
•setLen •getLen process:
•setWid •getWid a=l*w
•computeArea •computePeri p = 2*l + 2*w
•printAll decision:
0.0 < l,w < 20.0

Data of class rectangle:


•len, wid
•area, peri
24
//rech.h
#ifndef rech_H rectangle header SFG
#define rech_H Sal F. Gambino
class rectangle CS-342
{ public: rectangle(float = 1, float = 1);
void setLen(float);
void setWid(float);
void computeArea();
void computePeri();
void printAll();
float getLen();
float getWid();
private: float len, wid, area, peri;
};
#endif 25
// recimpl.cpp
rectangle implementation SFG
#include<iostream.h> Sal F. Gambino
#include "rech.h” CS-342
rectangle::rectangle(float l, float w)
{ setLen(l);
setWid(w);
}
void rectangle::setLen(float l)
{ len = (l > 0.0 && l < 20.0) ? l:1;
}
void rectangle::setWid(float w)
{ wid= (w > 0.0 && w < 20.0)? w:1;
} 26
rectangle implementation SFG
void rectangle::computeArea() Sal F. Gambino
CS-342
{ area = len * wid;
}
void rectangle::computePeri()
{ peri = 2 * len + 2 * wid;
}
void rectangle::printAll()
{
cout << "\nThe Area of the Rectangle is: " << area ;
cout << ”\n The Perimeter of the Rectangle is: " << peri;
}
27
SFG
rectangle implementation
Sal F. Gambino
CS-342

float rectangle::getLen()
{
return len;
}
float rectangle::getWid()
{
return wid;
}

28
rectangle driver SFG
#include<iostream.h> //recdriv.cpp Sal F. Gambino
#include "rech.h” CS-342
main()
{ rectangle t;
t.computeArea();
t.computePeri();
t.printAll();
cout << "\n (" <<t.getLen() << ", "
<< t.getWid() << " ) \n";
}
Output: The Area of the Rectangle is : 1
The Perimeter of the Rectangle is : 4
(1, 1 ) 29
rectangle driver SFG
#include<iostream.h> //recdriv.cpp
Sal F. Gambino
#include "rech.h”
CS-342
main()
{ rectangle t1(9,5);
t1.computeArea();
t1.computePeri();
t1.printAll();
cout << "\n (" <<t1.getLen() << ", "
<< t1.getWid() << " )\n";
}
Output: The Area of the Rectangle is : 45
The Perimeter of the Rectangle is : 28
( 9, 5 ) 30
rectangle driver SFG
#include<iostream.h> //recdriv.cpp
Sal F. Gambino
#include "rech.h”
CS-342
main()
{ rectangle t2 ( 3,7);
t2.computeArea();
t2.computePeri();
t2.printall();
cout << "\n (" <<t2.getLen() << ", "
<< t2.getWid() << " )\n";
}
Output: The Area of the Rectangle is: 21
The Perimeter of the Rectangle is: 20
( 3, 7 ) 31
#include<iostream.h> //recdriv.cpp SFG
#include "rech.h”
main() Sal F. Gambino
CS-342
{ rectangle t3(28,99);
cout << "\n ~InvalidParameters~~~";
rectangle
t3.computeArea(); driver
t3.computePeri();
t3.printAll();
cout << "\n (" <<t3.getLen() << ", "
<< t3.getWid() << " )";
}
Output: ~InvalidParameters~~~~";
The Area of the Rectangle is: 1
The Perimeter of the Rectangle is: 4
32
( 1, 1 )
//rech.h rectangle header 2 SFG
#ifndef rech_H
#define rech_H Sal F. Gambino
class rectangle CS-342

{ public: rectangle(float = 1, float = 1);


void setLen(float);
void setWid(float);
void printAll();
float getArea();
float getPeri();
float getLen();
float getWid();
private: float len, wid, area, peri;
};
#endif 33
rectangle implementation 2 SFG
#include "rech.h”
Sal F. Gambino
CS-342
float rectangle::getPeri() float rectangle::getArea()
{ {
peri = 2 * len + 2 * wid; area = len * wid;
return peri return area
} }
void rectangle::printAll()
{
cout << "\nThe Area of the Rect.. " << getArea ;
cout << ”\n The Perim. of the Rect.. " << getPeri;
} 34
rectangle driver 2 SFG
#include<iostream.h> //recdriv.cpp Sal F. Gambino
#include "rech.h” CS-342
main()
{ rectangle t2 ( 3,7);

t2.printall();
}

Output: The Area of the Rectangle is: 21


The Perimeter of the Rectangle is: 20
( 3, 7 )

35
Typical constructor with no arguments: SFG
Sal F. Gambino
CS-342
default is 0 default is 0 default is 1

time() vector() rectangle()


{ { { length = 1;
hrs = 0; xCo = 0; width = 1;
min = 0; yCo = 0; area = 1;
sec = 0; perim = 4;
} } }

Main driver: Main driver: Main driver:


time t; vector v; rectangle r;
36
<type> <class>:: getfunctionName() SFG
{
Sal F. Gambino
some process
CS-342
return functionName;
}
float rectangle::getArea()
{ Typical get function
area = len * width;
return area;
}

Driver pgm: rectangle r;


cout << “ The area is “ << r.getArea();
37
SFG
Typical set function or input data
Sal F. Gambino
There are many ways of entering data: CS-342
•by using CIN, input from the keyboard
•by using a random number generator function
•by using a constant or an assign operator
Rectangle r;
r.setLength();
Void rectangle::setLength()
{ // 0< length<20
length = rand()%19 + 1;
}
Private:
#include <time.h> float length;
38
SFG
Typical set function or input data
Sal F. Gambino
There are many ways of entering data: CS-342
•by using CIN, input from the keyboard
•by using a random number generator Rectangle
function r;
r.setLength();
•by using a constant or an assign operator

Void rectangle::setLength()
{ float len;
cout << “\n enter length “ ; cin<< len;
length = (len > 0.0 && len < 20.0) ? len : 1;
} Private:
float length; 39
SFG
Typical set function or input data
Sal F. Gambino
There are many ways of entering data: CS-342
•by using CIN, input from the keyboard
•by using a random number generator function
•by using a constant or an assign operator
Rectangle r;
Void rectangle::setLength(float len) r.setLength(15.0);
{
length = (len > 0.0 && len < 20.0) ? len : 1;
}
Private:
float length;
40

You might also like