Download presentation
Presentation is loading. Please wait.
Published byMelina Kelly Modified over 8 years ago
1
Objects and Class
2
Object-Oriented Programming (OOP) Procedure-oriented Programming Traditional method using command statements Imperative method Object-oriented Programming Views program as interacting objects Class—template for creating objects Class encapsulates data and behavior
3
Class Circle Class Name Circle Attributes radius Behavior Calculate area Calculate volume Circle radius Calculate area Calculate volume
4
Class Circle (cont.) class Circle{ public: Circle(double r){...} // create an object double getRadius(){...} // return radius double area(){...} // return area double volume(){... } // return volume private: double radius; }
5
Using Circle Class #include using namespace std; int main() { Circle cir1(3.0); // create circle obj. Circle cir2(5.0); // another object cout << “Circle of radius “ << 3 << “ has area of << cir1.area() << endl; cout << “Circle of radius “ << 5 << “ has volume of << cir2.volume() << endl; return 0; }
6
Encapsulation of Class Public section— Provides an interface to the outside Shows what methods are available in class But not how they are implemented Private section— Data structure hidden from the outside Implementation of methods are hidden “Hidden” means client programs cannot make use of the data structure or method implemenation
7
Separating Interface and Implementation // File: circle.h // Description: function prototypes class Circle{ public: Circle(double r); double getRadius(); double area(); double volume(); private: double radius; }; // Note semicolon
8
Separating Interface and Implementation (cont.) // File: circle.cpp // Description: function implementation Circle:: Circle(double r){... } double Circle::getRadius() {... } double Circle::area(){... } double Circle::volume() {... }
9
Separating Interface and Implementation (cont.) // File: circleTest.cpp // Description: Driver program to test Circle class #include #include “circle.h” using namespace std; int main() { Circle circ(3.0); // create object cout << “Circle of radius “ << 3 << “ has area of << circ.area() << endl; return 0; }
10
Class Triangle Class Name Triangle Attributes sideA, sideB, sideC Behavior Calculate angleA, angleB, angleC Calculate area setSideA, setSideB, setSideC Triangle sideA sideB sideC angleA angleB angleC area
11
Class Triangle (cont.) // File: triangle.h // Description: Triangle class interface class Triangle{ public: Triangle(); Triangle(double a, double b, double c); double getSideA(); double getSideB(); double getSideC(); double angleA(); double angleB(); double area(){...}... private: double a, b, c; }
12
Class Triangle (Implement.) // File: triangle.cpp // Description: Methods implementation #include “triangle.h” Triangle::Triangle(){...} Triangle::Triangle(doube a, double b, double c){...} double Triangle::getSideA(){...} double Triangle::getSideB(){...} double Triangle::getSideC(){...} double Triangle::angleA(){...} double Triangle::angleB(){...} double Triangle::area(){...}
13
Class Triangle (Driver) // File: triangleTest.cpp // Description: Driver to test Triangle class #include “triangle.h” #include using namespace std; int main(){ Triangle t(3, 4, 5); cout << “Angle A: “ << t.angleA() << endl; cout << “Angle B: “ << t.angleB() << endl; cout << “Angle C: “ << t.angleC() << endl; cout << “Area: “ << t.area() << endl; return 0; }
14
Method angleA() // Law of Cosines: // a 2 = b 2 + c 2 – 2bc x cos(A) // cos(A) = (b 2 + c 2 – a 2 )/2bc // A = cos -1 (cos(A)) where A is in radians const double PI = 3.14159; const double RAD2DEG = 180/PI; // returns angle A in degrees double angleA(){ double cosA = (b*b + c*c – a*a)/(2 * b * c); return acos(cosA) * RAD2DEG; }
15
Method area() // Heron’s Formula: // area = sqrt(s(s – a)(s – b)(s – c)) // where s = (a + b + c) / 2 #include // returns area of triangle double area(){ double s = (a + b + c) / 2; return sqrt(s * (s-a) * (s-b) * (s-c); }
16
Your Turn Create a Sphere class (interface and implementations in separate files). Test the class with a driver.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.