Вы находитесь на странице: 1из 3

DATA STRUCTURES AND ALGORITHMS

Lab 1: Basic Object-Oriented Programming with C++

Lab Preparation
First Program: Write the following program in your IDE and inspect the output.
#include<iostream>
using namespace std;

// main function - where the execution of program begins


int main()
{
cout<<"Hello World";
return 0;
}
Class, object, inheritance: Write the following program in your IDE.

#include <iostream>
using namespace std;

class Student
{
public:
string level;
int age;

Student(): level("secondary"), age(16) { }


void display()
{
cout << "My study level is: " << level << endl;
cout << "My age is: " << age << endl;
study();
exam();
}
void study() { cout << "I have to study." << endl; }
void exam() { cout << "I have SPM." << endl; }
};
1. Create a class Undergraduate Student as a derived class of Student with public
member function doProgramming() to output “I do programming”.
2. Override the function exam() in Undergraduate Student class so that it will output “I
have exam every semester”.
3. Override the function display() in Undergraduate Student class so that it will call local
member function exam().
4. Create a main function to test the base and derived class, to create new object of
Student, call display() and doProgramming().
Exercise 1: Inheritance concept.

1. Consider the following UML representation of a base class. Write a class called MobilePhone
and save as mobilephone.h. Note that function ‘getModel()’ will return model, ‘getPrice()’
will return price, and print() will print information of the phone.

MobilePhone
-id:int
-make: string
-model: string
-type: string = “smartphone”
-ram: string
-price: double
-stock: int
+getModel(): string
+getPrice():double
+setStock(): void
+print() const: void
+Mobilephone()
+Mobilephone(int,string, string, string, string, double): void

2. The following UML representation is a derived class IPhone of a MobilePhone. Create a class
called IPhone and save as iphone.h

IPhone
-os: string
-version: string
+set_specs(string, string)

3. The following UML representation is a derived class AndroidPhone of a MobilePhone.


Create a class AndroidPhone and save as androidphone.h

AndroidPhone
-os: string
-version:string
+set_specs(string, string)

Exercise 2: Implement the base and derived class.

Implement the definition of the member functions of all of the classes in (1). Then write a basic
program (main) that creates instances of IPhone and AndroidPhone using the details shown in
Table 1. Print the basic information of the mobile phone.
Table 1: Details of mobile phone data.
id Make Model RAM Price OS Version
1 IPhone IPhone 6S 2GB 1688.00 iOS 9.0
2 Samsung Galaxy S8 4GB 2756.00 Android 7.0
3 Motorola Moto Z2 4GB 1799.00 Android 7.1.1

Exercise 3: Overriding concept (redefining a member function of the base class).

1. Modify the definition of class IPhone and AndroidPhone to override the function print() in
MobilePhone and print their local variables.
2. Modify the overriding function print() to include phone information from MobilePhone.

Exercise 4: Overloading concept (Specifying more than one definition for a function name
or an operator in the same scope).

1. Function overloading:
a. Create setStock(int) function definition in MobilePhone with parameter (int) to accept
the current total stock number of a phone.
b. Implement the function by calling it from your program in Exercise 2.

2. Operator overloading: update MobilePhone class to implement a function to overload the


stream insertion operator (<<). The function should output the following example when the
operator is used in the program (Exercise 2):

#1 | Samsung | Galaxy S8 | 2GB | RM2756.00 | Stock = 100

Вам также может понравиться