Professional Documents
Culture Documents
Object Oriented Programming - Classes
Object Oriented Programming - Classes
BME1111 Computer Programming Language Lab 11-1 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 1:
Problem Description
Enter and run Program 11.1 in the textbook to practice basic class declaration and
implementation.
BME1111 Computer Programming Language Lab 11-2 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 1:
Guided Template
#include <iostream>
using namespace std;
// declaration section
class Complex
{
private: // notice the colon after the keyword private
double realPart; // data member
double imaginaryPart; // data member
// function prototypes
public: // again, notice the colon after the keyword public
Complex(double = 0.0, double = 0.0); // member function (constructor)
void assignNewValues(double, double); // member function
void showComplexValues(); // member function
}; // end of class declaration
BME1111 Computer Programming Language Lab 11-3 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 1: Guided Template
// implementation section
Complex::Complex(double real, double imag)
{
realPart = real;
imaginaryPart = imag;
answer1 = real; // for Autolab checking
answer2 = imag; // for Autolab checking}
void Complex::assignNewValues(double real, double imag)
{
realPart = real;
imaginaryPart = imag;
}
void Complex::showComplexValues()
{
char sign = '+';
if(imaginaryPart < 0) sign = '-';
cout << "The complex number is "
<< realPart << ' ' << sign << ' ' << abs(imaginaryPart) << "i\n";
}
BME1111 Computer Programming Language Lab 11-4 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 1:
Guided Template
int main()
{
Complex a, b, c(6.8,9.7); // declare 3 objects
return 0;
}
BME1111 Computer Programming Language Lab 11-5 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 1:
Sample Output
BME1111 Computer Programming Language Lab 11-6 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 2:
Problem Description
Enter and run Program 11.6 in the textbook to further practice class declaration and learn how
to process objects created from a class.
What to learn
• Member functions
• Adding class functions
• Inline member functions
• Accessor
• Mutator
BME1111 Computer Programming Language Lab 11-7 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 2:
Guided Template
#include <iostream>
using namespace std;
// declaration section
class Complex
{
private:
double realPart; // declare realPart as a double variable
double imaginaryPart; // declare imaginaryPart as a double variable
public:
Complex(double real = 0.0, double imag = 0.0) // inline constructor
{realPart = real; imaginaryPart = imag;}
void showComplexValues(); // accessor prototype
void assignNewValues(double real, double imag) // inline mutator
{realPart = real; imaginaryPart = imag;}
Complex multScaler(double = 1.0);
}; // end of class declaration
BME1111 Computer Programming Language Lab 11-8 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 2: Guided Template
// implementation section
void Complex::showComplexValues() // accessor
{
// Fill in your code here
}
Complex Complex::multScaler(double factor)
{
// Fill in your code here
answer1 = newNum.realPart;
answer2 = newNum.imaginaryPart;
}
int main()
{
Complex complexOne(12.57, 18.26), complexTwo; // declare two objects
cout << "The value assigned to complexOne is ";
complexOne.showComplexValues();
complexTwo = complexOne.multScaler(10.0); // call the function
cout <<"\nThe value assigned to complexTwo is ";
complexTwo.showComplexValues();
return 0;
}
BME1111 Computer Programming Language Lab 11-9 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 2:
Sample Output
BME1111 Computer Programming Language Lab 11-10 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 3:
Problem Description
Create a Time class and a driver program that tests the class. The class definition contains
prototypes for member functions Time, setTime, printTime. The class includes private integer
members hour, minute and second. Class Time’s private data members can only be accessed by
its member function. The driver program will ask the user to input the hour, minute and second.
The printTime function output the time in the following format.
BME1111 Computer Programming Language Lab 11-11 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 3:
Sample Output
BME1111 Computer Programming Language Lab 11-12 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 4:
Problem Description
Construct a class named Pol_coord containing two floating-point data members named dist and theta,
used to store the distance and angle values of a point in polar coordinates. The member functions should
include a constructor, a display function showData(), a mutator function updateData() that change the object
data values (dist and theta), and a function named convToPolar(). The convToPolar() function should accept two
floating-point numbers representing a point in Cartesian coordinates (x and y) and convert them into polar
coordinates (r and ). After conversion from Cartesian coordinates to polar coordinates, the data members dist
and theta should be updated using the mutator function updateData().
Write a driver program to ask the user for input of a point in Cartesian coordinates and then convert it to
polar coordinates. Display the conversion result using the showData() member function. For conversion from
Cartesian to polar coordinates, use these formulas:
BME1111 Computer Programming Language Lab 11-13 Dept. of Biomechatronics Engineering, NTU
Lab Exercise 4:
Sample Output
BME1111 Computer Programming Language Lab 11-14 Dept. of Biomechatronics Engineering, NTU