Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 14

LAB 11

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.

 AUTOLAB Submission Check:


double answer1; // Store the value of the real part of the last Complex object c
double answer2; // Store the value of the imaginary part of the last Complex object c
 What to learn
• C++ classes/objects
• Class construction
• Declaration section
• Implementation section
• Data members
• Member functions
• Constructor

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

// Assign new values to object b's data members


b.assignNewValues(5.3, -8.4);
a.showComplexValues(); // display object a's values
b.showComplexValues(); // display object b's values
c.showComplexValues(); // display object c's values

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.

 AUTOLAB Submission Check:


double answer1; // Store the value of the real part of the Complex object complexTwo
double answer2; // Store the value of the imaginary part of the Complex object complexTwo

 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.

Hour:Minute:Second (e.g. 15:30:20)

 AUTOLAB Submission Check:


int answer1; // Store the value hour
int answer2; // Store the value minute
int answer3; // Store the value second
 What to learn
• To get more familiar with the use of class

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:

 AUTOLAB Submission Check:


double answer1; // Store the value dist of the Pol_coord object a
double answer2; // Store the value theta of the Pol_coord object a

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

You might also like