Lab- 6 (Assignment).
Lab- 6 (Assignment).
LABORATORY WORK
ADILZHAN MUSTAFIN IT2-2102
Exercise 1
Create a class called Complex for performing arithmetic with complex numbers. Complex numbers
have the form real + i imag, where i = √-1.
Use floating point variables to represent the private data of the class.
Provide a constructor to initialize a complex number when it is declared.
The constructor should contain the default values in case no initializing values are provided.
Provide public member functions to perform the following operations:
• Addition of two Complex numbers
• Subtraction of two Complex numbers
• Multiplication of two Complex numbers
• Printing of a Complex number in a+ib format.
Write a driver program to test your class.
#include <iostream>
using namespace std;
class Complex {
private:
float real, imag;
public:
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
Complex add(const Complex& c) {
return Complex(real + c.real, imag + c.imag);
}
Complex subtract(const Complex& c) {
return Complex(real - c.real, imag - c.imag);
}
Complex multiply(const Complex& c) {
return Complex(real * c.real - imag * c.imag, real * c.imag + imag * c.real);
}
void print() const {
cout << real << " + " << imag << "i" << endl;
}
};
int main() {
Complex c1(3, 2), c2(1, 7);
Complex c3 = c1.add(c2);
c3.print();
c3 = c2.subtract(c1);
c3.print();
c3 = c2.multiply(c1);
c3.print();
return 0;
Exercise 2
Write a class to calculate the area and circumference of a circle.
Class Circle
Private:
float radius;
Public:
2
ComputeArea(); //Area:Πr
ComputeCircumference(); //Circumfernce:2Πr
#include <iostream>
using namespace std;
class Circle {
private:
float radius;
public:
Circle(float r = 0) : radius(r) {}
float computeArea() const {
return 3.14 * radius * radius;
}
float computeCircumference() const {
return 2 * 3.14 * radius;
}
};
int main() {
Circle c(5);
cout << "Area: " << c.computeArea() << endl;
cout << "Circumference: " << c.computeCircumference() << endl;
return 0;
}
#include <iostream>
using namespace std;
class Date {
private:
int month, day, year;
public:
void getDate() {
cout << "Enter date (MM/DD/YY): ";
char slash; // to ignore the '/' character in the input
cin >> month >> slash >> day >> slash >> year;
}
void showDate() const {
cout << month << "/" << day << "/" << year << endl;
}
};
int main() {
Date d;
d.getDate();
d.showDate();
return 0;
}
Exercise 4
Create a class called ship that incorporates a ship’s number and location in longitudes and latitudes. A
member function of the ship class should get a position from the user and store it in the object; another
should report the serial number and position.
Write a main() program that creates three ships, asks the user to input the position of each, and then
displays each ship’s number and position.
#include <iostream>
using namespace std;
class Ship {
private:
int serialNumber;
float longitude, latitude;
public:
void getPosition() {
cout << "Enter ship serial number, longitude, and latitude: ";
int main() {
Ship ships[3];
for (int i = 0; i < 3; i++) {
ships[i].getPosition();
}
for (int i = 0; i < 3; i++) {
ships[i].showPosition();
}
return 0;
}
Exercise 5:
Create an employee class. The member data should comprise an int for storing the employee
number and a float for storing the employee’s compensation. Member functions should allow the
user to enter this data and display it. Write main() function that allows the user to enter data for
three employees and display it (Use Array of objects).
#include <iostream>
using namespace std;
class Employee {
private:
int employeeNumber;
float compensation;
public:
void setData() {
cout << "Enter employee number and compensation: ";
cin >> employeeNumber >> compensation;
}
void showData() const {
cout << "Employee #" << employeeNumber << " Compensation: " << compensation <<
endl;
}
};
Exercise 6:
Create a C++ class named Sphere, which has one data members, radius of the sphere. Write set()
and get() methods to store and return the radius of the Sphere class. It also has a function named
Area() which calculates the area of the Sphere. Input the radius from the user and calculate the
area of the Sphere.
#include <iostream>
using namespace std;
class Sphere {
private:
float radius;
public:
void set(float r) { radius = r; }
float get() const { return radius; }
float Area() const { return 4 * 3.14159 * radius * radius; }
};
int main() {
Sphere s;
float radius;
cout << "Enter the radius of the sphere: ";
cin >> radius;
Exercise 7:
Create a class named myString,which consists of one string data member and three functions.
Input the string from the user at run time then call the following functions on it.
• void CountConsonants( ) //displays the total number of consonants in the string
• void VowelCount( ) //displays the total vowels in the string
• void Palindrome( ) //displays if the string is a palindrome or not
Use pointer of object to call the member function of the class.
#include <iostream>
#include <cstring>
using namespace std;
class myString {
private:
string str;
public:
void input() {
cout << "Enter a string: ";
cin >> str;
}
void CountConsonants() const {
int consonants = 0;
for (char c : str) {
if (!strchr("aeiouAEIOU", c) && isalpha(c)) {
System Level Programming
LAB 6
consonants++;
}
}
cout << "Consonants: " << consonants << endl;
}
void VowelCount() const {
int vowels = 0;
for (char c : str) {
if (strchr("aeiouAEIOU", c)) {
vowels++;
}
}
cout << "Vowels: " << vowels << endl;
}
void Palindrome() const {
string rev = string(str.rbegin(), str.rend());
if (str == rev) {
cout << "Palindrome: Yes" << endl;
} else {
cout << "Palindrome: No" << endl;
}
}
};
int main() {
myString s;
s.input();
s.CountConsonants();
s.VowelCount();
s.Palindrome();
return 0;
}
HINT 1