Class
Class
Class
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
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
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();
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
t2.printall();
}
35
Typical constructor with no arguments: SFG
Sal F. Gambino
CS-342
default is 0 default is 0 default is 1
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