Sie sind auf Seite 1von 12

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

Objective

LAB 1

At the end of this lab activity, the students should be able to:

Compile the C ++program using Microsoft Visual C++.

Run the program.

Write a simple C++ program.

Implement the main concepts in object-oriented programming.

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

Introducing compilation C++ using Microsoft Visual C++.

Microsoft Visual C++ 6.0

This section will explain how to edit, compile and run a C++ programme based on object,

using Microsoft Visual C++. To start Microsoft Visual C++:

Choose Start>All Programs>Microsoft Visual Studio 6.0 >Microsoft Visual C++ 6.0

Visual C++ 6.0 interface

∑ ∑ When you activate Microsoft Visual C++ 6.0, you will see its main screen. The
When you activate Microsoft Visual C++ 6.0, you will see its main screen.
The main screen consists of Title Bar, Menu Bar, shortcut keys, Standard Toolbar,
Minimize, Maximize, Close, Close window and project workspace window.
Close
Title bar
Minimize
Menu bar
6
1
4
2
3
5
Standard toolbar
Maximize
7
Project workspace window
9
Code window
8
Output window

Main screen of Microsoft Visual C++

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

Steps in developing, compiling and executing C++ program.

To develop, compile and run a C++ program, follow these steps:

  • i. In the main screen, choose the File menu, then click New

Menu File Menu item ( New ) Menu File
Menu File
Menu item ( New )
Menu File

The New Dialogue box will be displayed. Choose C++ source file and click OK.

Pilih Files dan C++ Source File Choose File and C++ Source File
Pilih Files dan C++ Source File
Choose File and C++ Source File

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

A blank window text editor will be displayed.

A blank text editor
A blank text editor

ii.

Type your source code C++ in windows texts editor.

#include<iostream.h> void main() { cout<<”Welcome to C++!”; cout<<”This is my first C++ program”; cout<<endl; }

DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 1 A blank window text editor will be displayed. A

All the keywords will be coloured with blue automatically

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

iii.

Save your file.

iv. Choose compile [filename] from Build menu. If you don’t have a workspace for your code, a dialogue box will be displayed where there will a question asking whether you would like to build a workspace.

DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 1 iii. Save your file. iv. Choose compile [filename] from

This is the dialogue box that will be displayed after the fourth steps taken

  • v. If there are any errors or warning messages in the program, Visual C++ will display it in the output window. If there are no errors or warning messages, you can execute your program.

Program with no errors

Program tanpa ralat dan amaran Program with free errors and warnings
Program tanpa ralat dan amaran
Program with free errors and warnings

The output window shows the program with free errors and warnings

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

vi.

Choose Execute [File name] from menu Built Output for program as shown

below.

DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 1 vi. Choose Execute [File name] from menu Built Output

Example of output program

If the program has an error, error message will be shown on the output window as
If the program has an error, error message will be shown on the output window as below. The
error should be corrected before executing the program.
There are two errors here
Error will be displayed here and you can check the errors line by
line with double clicks on the error

An output window that shows program with errors

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

Reminder: If you would like to compile a new program, you need to follow the instructions below.

Menu item (Close Workspace)
Menu item (Close Workspace)

Menu Fail

DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 1 Reminder: If you would like to compile a new

If you choose the Yes button, thus the previous program file will be closed accompanying with workspace too. On the other hand, if you choose No, thus only workspace for of the previous program will be closed and the file of the previous program will remain.

DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 1 Reminder: If you would like to compile a new
DCS 5088 OBJECT ORIENTED PROGRAMMING LAB 1 Reminder: If you would like to compile a new

The project area of the window workspace is blank , which shows that the workspace has been emptied.

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

LAB EXERCISE 1

Compile and run program

  • i) Type the program below. The purpose of this program is to show how to include data and display it on the screen.

#include<iostream.h> void main() { int a; cout<<”Enter one number:”; cin>>a; cout<<”The entered number is :”<<a; }

ii)

Save the program under the name of program1.cpp. Compile the program. If there is any error, correct it until the program is free of errors.

iii)

Modify your program, so

that

it

can

accept two numbers. After that,

compare both numbers to get which one structure.

is the biggest number using selection

iv)

Display the biggest number three times using looping structure.

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

LAB EXERCISE 2

Create a C++ program that accepts 3 inputs from user to calculate the Volume of a box. (Volume = Width * length *height)

i.

Create 3 variables of type float in your program

ii.

Your output may look something like this:

 

------------------------------------------

To calculate the volume of a box

------------------------------------------

Enter width, length, height 43.2 33.3 33.5

The volume of a box is 48191.8 Press any key to continue

iii.

Also, include proper source codes[use if statement] in your program so that it will only accept positive values. If the user enter negative values, the user will receive a message and could not get the volume displayed. The output may be something as below:

---------------------------------

To calculate the volume of a box

---------------------------------

Enter width, length, height -1 3 -22 you've entered negative values! Please try again another time Press any key to continue

Answer:

#include<iostream> using namespace std;

int main() { float width,length,height,volume; cout<<"---------------------------------"<<endl; cout<<"To calculate the volume of a box"<<endl; cout<<"---------------------------------"<<endl; cout<<"\nEnter width, length, height"<<endl;

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

cin>>width>>length>>height; if(width <0 || length <0 || height <0) { cout<<"you've entered negative values!"<<endl; cout<<"Please try again another time"<<endl;

} else{ volume=width*height*length; cout<<"\nThe volume of a box is "<<volume<<endl;

}

return 0;

}

 

iv.

Enhance your program so that even though the user enters a negative number, there is still chance to input another round of positive numbers. Use a while loop instead of if statement. Change the if statement to while loop. A sample output if you’ve managed to incorporate the while loop correctly to your program

 

---------------------------------

To calculate the volume of a box

---------------------------------

Enter width, length, height -3 22 33.1 you've entered negative values! Please try again another time

Enter width, length, height -4 -4.2 33 you've entered negative values! Please try again another time

Enter width, length, height 10.2 33.2 2

The volume of a box is 677.28 Press any key to continue

Answer:

#include<iostream> using namespace std;

int main() { float width,length,height,volume;

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1

cout<<"---------------------------------"<<endl; cout<<"To calculate the volume of a box"<<endl; cout<<"---------------------------------"<<endl; cout<<"\nEnter width, length, height"<<endl; cin>>width>>length>>height; while(width <0 || length <0 || height <0) { cout<<"you've entered negative values!"<<endl; cout<<"Please try again another time"<<endl; cout<<"\nEnter width, length, height"<<endl; cin>>width>>length>>height;

}

volume=width*height*length; cout<<"\nThe volume of a box is "<<volume<<endl;

return 0;

}

LAB EXERCISE 3

Enter the following codes in your C++ compiler and observe the output.

#include<iostream> using namespace std;

void main()

{

int number_days = 1; double daily_wage = 0.01, total_sum = 0.01; const double desired = 1000000.0;

while (total_sum < desired) { number_days++; daily_wage *=2; total_sum += daily_wage;

}

cout << "You will be a millionair after " << number_days << " days" <<endl; }

What is the output?

Rewrite the above program by using for loop. (“for(int i = 0; i <10; i ++)”)

Answer:

for(; total_sum < desired;number_days++) {

//number_days++; daily_wage *=2; total_sum += daily_wage;

}

DCS 5088 OBJECT ORIENTED PROGRAMMING

LAB 1