1. SANA'A UNIVERSITY
FACTUALY OF ENINEERING
MECHATRONICS DEPARTMENT
CLASSES REPORT LAB
NAME:
OSAMA HASAN ALMOHAIA
AC.NO 200/2017
SUPERVISORED BY:
ENG.ELHAM ABDULRAHMAN
2. Objective(s):
To be familiar with syntax and structure of OOP in C++.
To learn how to build first OOP of classes.
To learn about UML class diagram in solving problems using C++.
Software requirements
Title:
Write a class OOP Program that calculate the area and the perimeter of three geometric shapes (circle ,square
,tringle). Your program should be include three classes one for each shape and called from main function.
Problem Analysis:
The problem is to calculate the area and perimeter of three shapes having its inputs parameters identified as:
(rad)for circle, (L) for square, (a,b,c)for tringle and those variable are float type.
The output of the program display menu that mention three classes Square, Circle and Tringle . you should choose
either area or perimeter.
The area of square is Area= Length*Width
and the perimeter is perimete4*Lenght.
The area of circle is Area = 3.14 * (radius)2
and the perimeter is perimeter=2*3.1459*rad.
Also we can find the area of a triangle by using equation:
a = length of first side
b = length of second side
c = length of third side
s = (a + b+ c)/2
Area = sqrt(s*(s-a)*(s-b)*(s-c))
perimetr=a+b+c
-where sqrt stands for square roo
Algorithm:
Step 1: Start
Step 2: Declare three separate class Square,Circle,Tringle with data members and member functions.
Step 3: creat a constructor for each shape which has the same name of classes
Step 3: For each class Define and declare input variables to calculate the area and perimeter of square,
circle and tringle.
Step 4: Create an object for each class .
Step 5: Choose one shape (switch) of the three shape cases .
Step 6: In each case, call the classes objects .
Step 7: Read the inputs parameters of the shape and return its calculation value from its class.
Step 9: Stop
3. UML Class Diagram of the Program:
Square
- L:float
- x:int
-ch:char
"constructor"+Square():
+ret():char
Figure 1 UML class diagram indicating the class Square
Circle
-rad:float
+ total:float
-pi=3.1459:float
- x:int
-ch:char
"constructor"+Circle()
+ret():char
Figure 2 UML class diagram indicating the class Circle
Tringle
- a:float
-b:float
-c:float
-ch:char
- x:int
+s:float
+ar:float
+pr:float
"constructor"+Tringle()
+ret():char
Figure 3 UML class diagram indicating the class Tringle
4. CODING
(1) #include <iostream>
(2) #include<cmath>
(3) using namespace std;
(4) class Square
(5) {
(6) private:
(7) int x;
(8) char ch='1';
(9) float L;
(10) public:
(11) Square(){
(12) cout<<"Welcome to the square shape calculations:";
(13) while(ch=='1'){
(14) cout<<"nEnter '1' for area nEnter '2'for perimeter"<<endl;
(15) cin >>x;
(16) if (x==1)
(17) { cout<<" Enter the length:";
(18) cin>>L;
(19) cout<<"the Area of your square is: _"<<L*L<<"_n"<<endl;
(20) cout<<"nn press '0' To choose other shape npress '1'to again n press any key
to exit "<<endl;
(21) cin >>ch;
(22) cout<<"********************************"<<endl;
(23) }
(24) else if (x==2){
(25) cout<<" Enter the length:";
(26) cin>>L;
(27) cout<<"the premiter of your square is: _"<<L*4<<"_"<<endl;
(28) cout<<"nn press '0' To choose other shape npress '1'to again n press any key
to exit "<<endl;
(29) cin >>ch;
(30) } }}
5. (31) char ret()
(32) {
(33) return ch;
(34) }
(35) };
(36) class Circle
(37) { private:
(38) int x;
(39) char ch='1';
(40) float rad, pi=3.1459;
(41)
(42) public:
(43) float total;
(44) Circle(){
(45) cout<<"Welcome! to the circle shape calculation:";
(46) while(ch=='1'){
(47) cout<<"nEnter '1' for area nEnter '2'for perimeter"<<endl;
(48) cin >>x;
(49) if (x==1){
(50) cout<<" Enter the Radius:";
(51) cin>>rad;
(52) total=(2*rad*pi);
(53) cout<<"the area of your circle is: "<<total<<endl;
(54) cout<<"nn press '0' To choose other shape npress '1'to again n press any key
to exit "<<endl;
(55) cin >>ch;
(56) cout<<"********************************"<<endl;
(57) }
(58) if (x==2){
(59) cout<<"Enter the Radius:";
(60) cin>>rad;
(61) total=(rad*rad*pi);
(62) cout<<"the perimeter of your circle is: "<<total<<"n"<<endl;
6. (63) cout<<"nn press '0' To choose other shape npress '1'to again n press any key
to exit "<<endl;
(64) cin >>ch;
(65) cout<<"********************************"<<endl;
(66) }} }
(67)
(68) char ret()
(69) {
(70) return ch;
(71) }
(72) };
(73) class Tringle
(74) {
(75) private:
(76) int x;
(77) char ch='1';
(78) float a,b,c;
(79) public:
(80) float s,ar,pr;
(81) Tringle(){
(82) cout<<"welcome to the triangle shape calculation:";
(83) while(ch=='1'){
(84) cout<<"nEnter '1' for area nENter '2'for perimeter"<<endl;
(85) cin >>x;
(86) if (x==1){
(87) cout<<" Enter the length a:";
(88) cin>>a;
(89) cout<<"Enter the length b:";
(90) cin>>b;
(91) cout<<"Enter the length c:";
(92) cin>>c;
(93) s=(a+b+c)/2;
(94) ar=sqrt(s*(s-a)*(s-b)*(s-c));
(95) cout<<"the area of your triangle is: "<<ar<<endl;
7. (96) cout<<"nn press '0' To choose other shape npress '1'to again n press any key to
exit "<<endl;
(97) cin >>ch;
(98) cout<<"********************************"<<endl;
(99) }
(100) else if (x==2)
(101) {
(102) cout<<" Enter the length a:";
(103) cin>>a;
(104) cout<<"Enter the length b:";
(105) cin>>b;
(106) cout<<"Enter the length c:";
(107) cin>>c;
(108) pr=a+b+c;
(109) cout<<"the perimeter of your triangle is: "<<pr<<endl;
(110)
(111) cout<<"nn press '0' To choose other shape npress '1'to again n press any key
to exit "<<endl;
(112) cin >>ch;
(113) cout<<"********************************"<<endl;
(114) }}}
(115) char ret()
(116) {
(117) return ch;
(118) }
(119) };
(120)
(121)
(122) int main()
(123) { char y='0';
(124) char ch;
(125) while(y=='0'){
(126) cout << "Hello!n please choose one shape of the following :n1- square.n2-
circle.n3- trianglenpress any other key to exitn." << endl;
8. (127) cin>>ch;
(128) if(ch=='1'){
(129) Square S;
(130) y=S.ret();
(131) }
(132) else if(ch=='2'){
(133) Circle C;
(134) y=C.ret();
(135) }
(136) else if(ch=='3'){
(137) Tringle T;
(138) y=T.ret();
(139) }
(140) else
(141) { cout<<"The program will close ....enter to continue"<<endl;
(142) y='5';
(143) }}}
Discussions:
(1) #include <iostream>
(2) #include<cmath>
(3) using namespace std;
From now we can treat with c++ so, we include the standard c++ library <iostream>
And <cmath> to calculate the area of shapes.
We you start the program it will display the main function
In the main function we declare about objects of each class shape and display menu to choose one shape for
calculation.
When you choose one option (i.e 1. square) the main go to class data type square and perform the operation and
method in this class.
Firstly, function method void sq() initialized and display welcome message attend you to enter the input parameter
and give you two option either area or perimeter .
Each case perform its calculation and the total return to the main function.
9. (4) class Square
(5) {
(6) private:
(7) int x;
(8) char ch='1';
(9) float L;
(10) public:
(11) Square(){
(12) cout<<"Welcome to the square shape calculations:";
(13) while(ch=='1'){
(14) cout<<"nEnter '1' for area nEnter '2'for perimeter"<<endl;
(15) cin >>x;
(16) if (x==1)
(17) { cout<<" Enter the length:";
(18) cin>>L;
(19) cout<<"the Area of your square is: _"<<L*L<<"_n"<<endl;
(20) cout<<"nn press '0' To choose other shape npress '1'to again n press any key to exit
"<<endl;
(21) cin >>ch;
(22) cout<<"********************************"<<endl;
(23) }
(24) else if (x==2){
(25) cout<<" Enter the length:";
(26) cin>>L;
(27) cout<<"the premiter of your square is: _"<<L*4<<"_"<<endl;
(28) cout<<"nn press '0' To choose other shape npress '1'to again n press any key to exit
"<<endl;
(29) cin >>ch;
(30) } }}
(31) char ret()
(32) {
(33) return ch;
(34) }
(35) };
10. Line 4to 35 show the Declaration of Square class.
Line 4,the Square class definition should be declared firstly and this class contain data members (private for the
parameter input of square shape and public for the function member (method) which perform calculations)
Note: Reading and calculating and Display the information for square shape done in Constructor Square()-line11.
After you complete the calculation, the program tell you if you want to calculate another shape press certain
number shown in the screen and if you want to exit press any key.
These number entered recently return to main function by the member function char ret()(Line 40).
Output (Compilation, Debugging & Testing)
The main menu output screen .
Figure 2 the main menu output screen
Choose one option let we choose 3-tringle
Figure 3 the tringle menu
to calculate area press 1 and for perimeter press 2
the program tell you to enter a , b and c
Figure 4 the tringle parameters input screen
11. If you want to do another calculation for the same shape press0 or 1 to another shape or any key to exit
Conclusion
This is the first program written in abstract Data type OOP classes C++. The program is focused on the calculation
of area and perimeter of three shape.
From this lab, I understood the basic classes declaration and definition including data member and method.
References:
C++ - How to Program 6th ed. - P. Deitel, et. al., (Pearson, 2010).
Lab sheet.
www.researchgate.net/publication/322908864_C_Programming_Lab_Manual .
www.google.com.