Use of Scope Resolution Operator

Use of Scope Resolution Operator
Use of Scope Resolution Operator
Before reading this please go through my last Post Click here to view .

1 // This program demonstrates a simple class with member functions
2 // defined outside the class declaration.
3 #include <iostream>
4 #include <cmath>
5 using namespace std;
6
7 // Circle class declaration
8 class Circle
9 { private:
10 double radius; // This is a member variable.
11
12 public:
13 void setRadius(double); // These are just prototypes
14 double getArea(); // for the member functions.
15 };
16
17 // The member function implementation section follows. It contains the
18 // actual function definitions for the Circle class member functions.
19
20 /*******************************************************************
21 * Circle::setRadius *
22 * This function copies the argument passed into the parameter to *
23 * the private member variable radius. *
24 *******************************************************************/
25 void Circle::setRadius(double r) // Use of Scope Resolution Operator
26 { radius = r;
27 }
28
29 /******************************************************************
30 * Circle::getArea *
31 * This function calculates and returns the Circle object's area. *
32 * It does not need any parameters because it already has access *
33 * to the member variable radius. *
34 ******************************************************************/
35 double Circle::getArea() // Use of Scope Resolution Operator
36 { return 3.14 * pow(radius, 2);
37 }
38
39 /******************************************************************
40 * main *
41 ******************************************************************/
42 int main()
43 {
44 Circle circle1, // Define 2 Circle objects
45 circle2;
46
47 circle1.setRadius(1); // This sets circle1's radius to 1
48 circle2.setRadius(2.5); // This sets circle2's radius to 2.5
49
50 // Get and display each circle's area
51 cout << "The area of circle1 is " << circle1.getArea() << endl;
52 cout << "The area of circle2 is " << circle2.getArea() << endl;
53
54 return 0;

55 }
Please First try Dry_running this program and then compile to check your efficiency .
Previous
Next Post »