DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
LAB 7
Objective
At the end of this this lab activity, the students should be be able to:
•
Implement the main concepts in object-oriented programming.
•
Using friend functions and friend classes.
•
Introduction to using new and delete operators
MULTIMEDIA UNIVERSITY
1
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
EXERCISE 1 #include #include class Square { private: int length; public: void set_data(int x) { length = x; } int area() { return pow(length,2);
}
};
Based on the program above, modify it to include the following (a)
a member function that returns the volume of a square object
(b)
a default constructor that initializes the length of the object to 2.
(c)
a destructor that prints a statement, ex: "destructing object with length 2”
(d)
in the main function, do the following in order: (i)
create an object using the new operator
(ii)
Invoke area() and volume() functions on the object appropriately to print out the values.
(iii)
Delete the object in (i).
(iv)
Create an dynamic array of 5 objects using the new operator
(v)
Using a for loop, call some functions on each object in (iv), •
Invoke the set_data(..) function passing the value of the counter + 1
•
invoke the area() and volume() functions on each object appropriately to print out the values.
(vi)
Delete the object in (iv).
MULTIMEDIA UNIVERSITY
2
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
Sample Output;
Area is 4 Volume is 8 destructing object with length 2 ===================================== Area is 1 Volume is 1 Area is 4 Volume is 8 Area is 9 Volume is 27 Area is 16 Volume is 64 Area is 25 Volume is 125 destructing object with length destructing object with length destructing object with length destructing object with length destructing object with length Press any key to continue
5 4 3 2 1
SOLUTION EXERCISE 1 #include #include class Square { private: int length; public: Square( ) { length=2;}; // default constructor ~Square() { cout<<"destructing object with length "<
}
int volume() { return pow(length,3); } }; void main() { Square *SQ;
MULTIMEDIA UNIVERSITY
3
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
SQ=new Square; cout<<"Area is "<area()<volume()<
delete [] SQ; }
MULTIMEDIA UNIVERSITY
4
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
EXERCISE 2 (a)
create a class called PriceTag
(b)
data members : price(float), colour (string), barcodeno(string)
(c)
public member functions define outside: (i)
void display() to display the values of the data members.
(ii)
void chgbarcodeno (…) to change the barcodeno to a value from main() that is set by user
(iii)
a default constructor that uses initialization list to set the price, colour and barcodeno to 1.50, “Beige”, "XNE121010" respectively and prints out , example “Price tag for XNE121010 created”
(iv)
a parameterized constructor that uses initialization list to set the price, colour and barcodeno to the values passed from main and prints out , example “Price tag for GRE301155 created”
(v) (d)
destructor that prints , example “destructed XNE121010”.
In main() function: (i)
create an object called using new operator (this should invoke the default constructor.
(ii)
prompt user to change barcode no. if yes then user to enter new barcode no and call chgbarcodeno() passing the new barcode no.
(iii)
call function display()
(iv)
delete the object
(v)
create another object called using new operator (this should invoke the parameterized constructor, so pass some arguments. Example 2.50,"Green","GRE301155n")
(e)
(vi)
call function display() on the 2nd object
(vii)
delete the 2nd object
Compile and run the program. Below is the sample output; Price tag for XNE121010 created Change barcode no? [y/n] n price = 1.5 colour = Beige barcode no = XNE121010 destructed XNE121010 Price tag for GRE301155 created price = 2.5 colour = Green barcode no = GRE301155 destructed GRE301155 Press any key to continue MULTIMEDIA UNIVERSITY
5
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
SOLUTION EXERCISE 2 #include #include using namespace std; class PriceTag { private: float price; string colour, barcodeno; public: PriceTag(); PriceTag(float, string, string); ~PriceTag(); void display(); void chgbarcodeno(string);
}; PriceTag::PriceTag():price(1.50),colour("Beige"),barcodeno("XNE121010") {cout<<"Price tag for "<
void PriceTag::chgbarcodeno(string bcn) { barcodeno=bcn; }
void main() { char select; string bcno; PriceTag *ptr=new PriceTag; cout<<"Change barcode no? [y/n]"; cin>>select; if(select=='y')
MULTIMEDIA UNIVERSITY
6
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
{ cout<<"Enter new barcode no"<>bcno; (*ptr).chgbarcodeno(bcno); } (*ptr).display(); delete ptr; cout<
MULTIMEDIA UNIVERSITY
7
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
EXERCISE 3 1. Create a class called CoreographMarks (a)
Data members : points and total of float type
(b)
Member functions :
(i)
default constructor : that initializes total to 30
(ii)
void setCgPoints() •
(c)
set the points based on user input
class FigureSkater is a friend of this class
2. Create a class called ArtisticMarks (a)
Data members : points and total of float type
(b)
Member functions :
(i)
default constructor : that initializes total to 20
(ii)
void setArtPoints() •
(c)
set the points based on user input
class FigureSkater is a friend of this class
3. Create a class called FigureSkater (a)
Data members : name of string type, age of int type and finalpoints, cpoints, apoints of float type
(b)
Member functions :
(i)
void set_details() •
(ii)
set details (name, and age) for a figureskater
void calcFinalPoints(…, …) •
takes an object of CoregraphMarks and an object of ArtisticMarks as arguments.
•
This function initializes the cpoints(in %) and apoints(in %) to the actual marks of the coreograph points and artistic points. Use this formula: Actual Points= (Points/ Total)*100;
•
Calculates the finalpoints (should be in %) based on the raw points(addition of points from coreographmarks object and artisticmarks object) divide by total points(addition of total from coreographmarks object and artisticmarks object)
(iii)
getAge
MULTIMEDIA UNIVERSITY
8
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
•
(iv)
returns the age
getName •
(v)
returns the name(
displayScoreDetails() •
displays ChoregraphyMarks, ArtisticMarks and Final Points, (all in %), using the appropriate data members.
4. In main() function (i)
prompt user to enter number of figure skaters to be created
(ii)
create a dynamic array of objects based on the size set by user in (i) , and new operator
(iii)
create an object of CoreographMarks
(iv)
create an object of ArtisticMarks
(v)
using a for loop, •
call the appropriate method to set details for each figure skater
•
set the points for the coreograph and artistic object each using the appropriate method.
•
Display the name and age of the figureskater using the appropriate method
•
Call displayScoreDetails() using the dynamic object element to display the details of the score
(vi)
delete the dynamic array of objects created in (ii).
Sample Output: Enter number of figure skaters 3 :::::Setting details for figure skater::::: Enter name
: Jenny Penn
Enter age
: 21
Enter Coreograph Marks
[max:30]: 19
Enter Artistic Marks
[max:20]: 15
----------------------------------------========================================= Figure Skater 1 ========================================= Name
= Jenny Penn
Age
= 21
MULTIMEDIA UNIVERSITY
9
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
:::::Details of scoring::::: Coreography
:63.3333
Artistic
:75
Final score
:68
========================================= ----------------------------------------:::::Setting details for figure skater::::: Enter name
: Pauline Ong
Enter age
: 20
Enter Coreograph Marks
[max:30]: 27
Enter Artistic Marks
[max:20]: 17
----------------------------------------========================================= Figure Skater 2 ========================================= Name
= Pauline Ong
Age
= 20
:::::Details of scoring::::: Coreography
:90
Artistic
:85
Final score
:88
========================================= ----------------------------------------:::::Setting details for figure skater::::: Enter name
: Iris Lee
Enter age
: 19
Enter Coreograph Marks
[max:30]: 18
Enter Artistic Marks
[max:20]: 14
----------------------------------------========================================= Figure Skater 3 ========================================= Name
= Iris Lee
Age
= 19
:::::Details of scoring:::::
MULTIMEDIA UNIVERSITY
10
DCS 5088 OBJECT ORIENTED PROGRAMMING
Coreography
:60
Artistic
:70
Final score
:64
LAB 7
========================================= -----------------------------------------
Press any key to continue
SOLUTION EXERCISE 3 #include #include using namespace std; class CoreographMarks { private: float points, total; public: CoreographMarks() { total=30; } void setCgPoints() { cout<<"Enter Coreograph Marks\t [max:30]: "; cin>>points; } friend class FigureSkater; }; class ArtisticMarks { private: float points, total; public: ArtisticMarks() { total=20; } void setArtPoints() { cout<<"Enter Artistic Marks\t [max:20]: "; cin>>points; } friend class FigureSkater; }; class FigureSkater {
MULTIMEDIA UNIVERSITY
11
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
private: string name; int age; float finalpoints, cpoints, apoints; public: void set_details() { cin.ignore(); cout<<":::::Setting details for figure skater::::: "<>age; } void calcFinalPoints(CoreographMarks c, ArtisticMarks a) { cpoints=(c.points/c.total)*100; apoints=(a.points/a.total)*100; finalpoints=((c.points+a.points)/(c.total+a.total))*100; } string getName() { return name; } int getAge() { return age; } void displayScoreDetails() { cout<<"\n\n:::::Details of scoring:::::"<>num; cout<
MULTIMEDIA UNIVERSITY
12
DCS 5088 OBJECT ORIENTED PROGRAMMING
LAB 7
cout<<"========================================="<
}
MULTIMEDIA UNIVERSITY
13
DCS 5088 OBJECT ORIENTED PROGRAMMING
MULTIMEDIA UNIVERSITY
LAB 7
14