Here is the sample application of Functions and Classes in c++.
Things
to note:
1.
Identify
the object or class
2.
Identify
its properties
3.
Identify
its functions: usually they are the following:
a.
Function
of each input
b.
Function
for calculations and display
4.
Then,
define and order them in the program
Click the jump for an example.
Create
a program that would ask the user for the radius of a circle and display its
area and circumference.
The
things to note would be
1.
Object:
Circle
2.
Properties:
radius, area, circumference & pi(3.1416)
3.
Functions
a.
1
function for input radius
b.
2
functions for calculate and output (radius and circumference)
4.
First
input, calculate and then output.
Project:
Circle
------------------------------------------------------------------------------------------------------------------------------------------
#include
"stdafx.h" //include
libraries
#include
<iostream>
#include
<string>
using
namespace std;
const
double pi = 3.1416; //declared outside since global and constant
class
Circle { //define the class in
this format
private: //declare properties of class in private
double radius, area, circumference;
public:
void setRadius(); //func to set Radius. void since it won’t return any
double calcArea(); //func to return the value of area
double calcCircumference(); //function to
return the circumference
};
int
main(){
Circle c1, c2;//declare two objects of the class Circle, c1 and c2
c1.setRadius(); //input for radius of c1
//calculate for area of c1
//calculate for area of c1
cout << "The area of c1 is
" << c1.calcArea();
//calculate circumference of c1
//calculate circumference of c1
cout << "\nThe circumference if
c1 is " << c1.calcCircumference();
c2.setRadius(); //input for radius of c2
//calculate for area of c2
//calculate for area of c2
cout << "The area of c2 is
" << c2.calcArea();
//calculate circumference of c1
//calculate circumference of c1
cout << "\nThe circumference if
c2 is " << c2.calcCircumference();
system("pause");
return 0;
}
void
Circle::setRadius(){
cout << "\n\nEnter the radius
of the circle: ";
cin >> radius;
}
double
Circle::calcArea(){
area = pi * radius * radius;
return area; //when calling, return the value of area
}
double
Circle::calcCircumference(){
circumference = 2 * pi * radius;
return circumference; //when calling, return the circumference
}
------------------------------------------------------------------------------------------------------------
Based
on the sample, create the following programs with the use of class and
functions:
1.
Project:
Triangle
Ask for the base and the height of a triangle, calculate
and display its area. (a = ½ bh)
2.
Project:
calcAge
Ask the user for his first name, last name, and birthday
in days, month and year, then display the age of the user(assuming that today
is March 5, 2013).
Test Case1:
Enter first name: Jen
Enter last name: Lerio
Birthday: Day: 13
Month: 1
Year: 2003
Jen Lerio is 10 years old
Test Case 2:
Enter first name: Jack
Enter last name: Cruz
Birthday: Day: 12
Month: 12
Year: 1993
Jack Cruz is 19 years old
No comments:
Post a Comment