Escolar Documentos
Profissional Documentos
Cultura Documentos
LAB 7
LAB 7
Objective At the end of this lab activity, the students should 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
LAB 7
EXERCISE 1
#include<iostream.h> #include <math.h> 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) (b) (c) (d) a member function that returns the volume of a square object a default constructor that initializes the length of the object to 2. a destructor that prints a statement, ex: "destructing object with length 2 in the main function, do the following in order: (i) (ii) (iii) (iv) (v) create an object using the new operator Invoke area() and volume() functions on the object appropriately to print out the values. Delete the object in (i). Create an dynamic array of 5 objects using the new operator Using a for loop, call some functions on each object in (iv), (vi) 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. Delete the object in (iv).
MULTIMEDIA UNIVERSITY
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<iostream.h> #include <math.h> class Square { private: int length; public: Square( ) { length=2;}; // default constructor ~Square() { cout<<"destructing object with length "<<length<<endl; } void set_data(int x) { length = x; } int area() { return pow(length,2); int volume() { return pow(length,3); } }; void main() { Square *SQ; }
MULTIMEDIA UNIVERSITY
LAB 7
SQ=new Square; cout<<"Area is "<<SQ->area()<<endl; cout<<"Volume is "<<SQ->volume()<<endl; delete SQ; cout<<"====================================="<<endl; SQ=new Square[5]; for(int i=0; i<5; i++) { SQ[i].set_data(i+1); cout<<"Area is "<<SQ[i].area()<<endl; cout<<"Volume is "<<SQ[i].volume()<<endl; cout<<endl; } delete [] SQ; }
MULTIMEDIA UNIVERSITY
LAB 7
EXERCISE 2 (a) (b) (c) create a class called PriceTag data members : price(float), colour(string), barcodeno(string) public member functions define outside: (i) (ii) void display() to display the values of the data members. 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
created tag for XNE121010
(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 XNE121010.
(v)
(d) In main() function: (i) (ii) (iii) (iv)
create an object called using new operator (this should invoke the default constructor. prompt user to change barcode no. if yes then user to enter new barcode no and call chgbarcodeno() passing the new barcode no. call function display() delete the object create another object called using new operator (this should invoke the parameterized constructor, so pass some arguments. Example
2.50,"Green","GRE301155n")
(v)
call function display() on the 2nd object delete the 2nd object
LAB 7
SOLUTION EXERCISE 2
#include<iostream> #include<string> 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 "<<barcodeno<<" created"<<endl;} PriceTag::PriceTag(float p, string c, string bc):price(p), colour(c),barcodeno(bc) {cout<<"Price tag for "<<barcodeno<<" created"<<endl;} PriceTag::~PriceTag(){ cout<<"destructed "<<barcodeno<<endl; } void PriceTag::display() { cout<<"price\t\t = "<<price<<endl; cout<<"colour\t\t = "<<colour<<endl; cout<<"barcode no\t = "<<barcodeno<<endl; } 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
LAB 7
{ cout<<"Enter new barcode no"<<endl; cin>>bcno; (*ptr).chgbarcodeno(bcno); } (*ptr).display(); delete ptr; cout<<endl; PriceTag *qtr=new PriceTag(2.50,"Green","GRE301155n"); (*qtr).display(); delete qtr; }
MULTIMEDIA UNIVERSITY
LAB 7
EXERCISE 3 1. Create a class called CoreographMarks (a) (b) Data members : points and total of float type Member functions : (i) (ii) default constructor : that initializes total to 30 void setCgPoints() (c) set the points based on user input
2. Create a class called ArtisticMarks (a) (b) Data members : points and total of float type Member functions : (i) (ii) default constructor : that initializes total to 20 void setArtPoints() (c) set the points based on user input
3. Create a class called FigureSkater (a) (b) Data members : name of string type, age of int type and finalpoints, cpoints, apoints of float type 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
LAB 7
(iv) (v)
displayScoreDetails() displays ChoregraphyMarks, ArtisticMarks and Final Points, (all in %), using the appropriate data members.
4. In main() function (i) (ii) (iii) (iv) (v) prompt user to enter number of figure skaters to be created create a dynamic array of objects based on the size set by user in (i) , and new operator create an object of CoreographMarks create an object of ArtisticMarks using a for loop, (vi) 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 delete the dynamic array of objects created in (ii).
Sample Output:
Enter number of figure skaters 3 :::::Setting details for figure skater::::: Enter name Enter age Enter Coreograph Marks Enter Artistic Marks : Jenny Penn : 21 [max:30]: 19 [max:20]: 15
MULTIMEDIA UNIVERSITY
LAB 7
========================================= ----------------------------------------:::::Setting details for figure skater::::: Enter name Enter age Enter Coreograph Marks Enter Artistic Marks : Pauline Ong : 20 [max:30]: 27 [max:20]: 17
========================================= ----------------------------------------:::::Setting details for figure skater::::: Enter name Enter age Enter Coreograph Marks Enter Artistic Marks : Iris Lee : 19 [max:30]: 18 [max:20]: 14
:::::Details of scoring:::::
MULTIMEDIA UNIVERSITY
10
LAB 7
SOLUTION EXERCISE 3
#include<iostream> #include<string> 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
LAB 7
private: string name; int age; float finalpoints, cpoints, apoints; public: void set_details() { cin.ignore(); cout<<":::::Setting details for figure skater::::: "<<endl; cout<<"Enter name\t\t\t : "; getline(cin, name); cout<<"Enter age\t\t\t : "; cin>>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:::::"<<endl; cout<<"Coreography\t:"<<cpoints<<endl; cout<<"Artistic\t:"<<apoints<<endl; cout<<"Final score\t:"<<finalpoints<<endl; } }; void main() { int num; cout<<"Enter number of figure skaters"<<endl; cin>>num; cout<<endl; FigureSkater *p=new FigureSkater[num]; CoreographMarks cm; ArtisticMarks am; for(int i=0; i<num; i++) { p[i].set_details(); cm.setCgPoints(); am.setArtPoints(); p[i].calcFinalPoints(cm,am); cout<<"-----------------------------------------"<<endl;
MULTIMEDIA UNIVERSITY
12
LAB 7
cout<<"========================================="<<endl; cout<<"Figure Skater "<<i+1<<endl; cout<<"========================================="<<endl; cout<<"Name\t= "<<p[i].getName()<<endl; cout<<"Age\t= "<<p[i].getAge(); p[i].displayScoreDetails(); cout<<"========================================="<<endl; cout<<"-----------------------------------------"<<endl; cout<<endl; } delete [] p; }
MULTIMEDIA UNIVERSITY
13
LAB 7
MULTIMEDIA UNIVERSITY
14