0% found this document useful (0 votes)
8 views7 pages

Laboratory Manual

The document is a laboratory manual for the Programming Language II course focused on Object-Oriented Programming in C++. It outlines various programming tasks and exercises in C++, covering topics such as basic program construction, control statements, structures, functions, classes, arrays, operator overloading, and inheritance. Each section includes specific tasks, exercises, and examples to help students understand and apply programming concepts.

Uploaded by

mellorinechan38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

Laboratory Manual

The document is a laboratory manual for the Programming Language II course focused on Object-Oriented Programming in C++. It outlines various programming tasks and exercises in C++, covering topics such as basic program construction, control statements, structures, functions, classes, arrays, operator overloading, and inheritance. Each section includes specific tasks, exercises, and examples to help students understand and apply programming concepts.

Uploaded by

mellorinechan38
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

LABORATORY MANUAL REPORT

IT-22014

PROGRAMMING LANGUAGE II

OBJECT-ORIENTED PROGRAMMING IN C++

MAY, 2019

DEPARTMENT OF COMPUTER ENGINEERING

AND INFORMATION TECHNOLOGY

YANGON TECHNOLOGICAL UNIVERSITY


Information of Lab Manual (IT 22014 Programming Language II)
Contact
No Activities
Hours
1 P1: Basic C++ Program Construction 2
Task:
 To understand an overview of
- what is C++
- what is the difference between C and C++
- how can write C++ program
- how can define constants, variables and keywords
 To analyze the conversion of Data Types, arithmetic operators, and
library functions
 Experiments by using Dev-C++ software or C-Free5 software

Exercise1:
Write a program that asks the user to enter a number of gallons, and then
displays the equivalent in cubic feet. (7.481 gallons in a cubic foot)
Exercise2:
Write a program that allows the user to enter a letter, and then displays
either zero or nonzero, depending on whether a lowercase or uppercase
letter was entered.
2 P2: Control Statements: Operations of Loop and Decisions 5

Task:
 To study the control statements and selection structure
 Experiments the decision making statements by using Dev-C++
software

Exercise1:

Write a program that allows the user to enter the number and then generates
the table, formatting it into 10 columns and 20 lines.

Enter a number: 7
7 14 21 28 35 42 49 56 63 70
77 84 91 98 105 112 119 126 133 140
147 154 161 168 175 182 189 196 203 210

Exercise2:

Create a four-function calculator. The program should ask the user to enter a
number, an operator, and another number. It should then carry out the
specified arithmetical operation: adding, subtracting, multiplying, or
dividing the two numbers. Use a switch statement to select the operation.
Finally, display the result.
Some sample interaction with the program might look like this:
Enter first number, operator, second number: 10/3
Answer = 3.333333
Do another (y/n)? y
Enter first number, operator, second number: 12 + 1000
Answer = 112
Do another (y/n)? n

Exercise3:

Write a program that calculates how much money you’ll end up with if you
invest an amount of money at a fixed interest rate, compounded yearly.
Have the user furnish the initial amount, the number of years, and the yearly
interest rate in percent. Some interaction with the program might look like
this:

Enter initial amount: 3000


Enter number of years: 10
Enter interest rate (percent per year): 5.5
At the end of 10 years, you will have 5124.43 dollars

3 P3: Analysis of Structures 3

Task:
 To study the structures that is a group of data items of different data
types held together in a single unit
 Experiments with Structures using Dev-C++

Exercise1:
Write a program that uses a structure called Phone. A phone number, such
as (212) 767-8900, can be thought of as having three parts: the area code
(212), the exchange (767), and the number (8900). The interchange might
look like this:
Enter your area code, exchange, and number: 415 555 1212
My number is (212) 767-8900
Your number is (415) 555-1212
Exercise2:
Create Date structure which contains day, month and year member data.
Write a program which accepts member data from keyboard and display
them.
Exercise3:
Create Library_book structure which contains a Book structure and a Date
structure. Write a program for creating Library_book array for three books
and display that array.
4 P4: Constructing the Functions 4
Task:
 To study the functions that consist of a sub-unit of a program can
perform a specific task
 To solve the more complex problems
 Experiments with Functions using Dev-C++

Exercise1:

Write a function called zeroSmaller() that is passed two int arguments by


reference and then sets the smaller of the two numbers to 0. Write a main()
program to exercise this function.

Exercise2:

Write a function that takes two Distance values as arguments and returns
the larger one. Include a main() program that accepts two Distances values
from the user, compares them, and displays the larger.

Exercise3:
Write a program to generate Fibonacci number with the recursive function
call.

Exercise4:
Find the sum of 1!/1^1+2!/2^2+3!/3^3+…….n!/n^n

5 P5: Learning Objects and Classes 4

Task:
 To study the concept of a class and object
 To develop a basic class with fundamental data members
 To use a constant member function
 Experiment with classes and objects using Dev-C++

Exercise1:

Create a class called time that has separate int member data for hours,
minutes, and seconds. One constructor should initialize this data to 0, and
another should initialize it to fixed values. Another member function should
display it, in 11:59:59 format. The final member function should add two
objects of type time passed as arguments.
A main () program should create two initialized time objects and one that
isn’t initialized. Then it should add the two initialized value together,
leaving the result in the third time variable. Finally it should display the
value of this third variable. Make appropriate member functions const.

Exercise2:
Create a class that contains four member functions, with 0, 1, 2 and 3 int
arguments, respectively. Create a main() that makes an object of that class
and calls each of the member functions. Now Modify the class so it has
instead a single member function with all the arguments defaulted. Does this
change the main()?

6 P6: Constructing Arrays and Strings 4

Task:
 To study the fundamentals of arrays and strings
 To study the arrays as object or class member data
 To study the strings as class member data
 Experiments with the strings as pass by value or pass by reference

Exercise1:

Write a function called reversit() that reverses a C-string (an array of char).
Use a for loop that swaps the first and last characters, then the second and
next-to-last characters, and so on. The string should be passed to reversit()as
an argument.
Exercise2:
Create a class called employee that contains a name (an object of class
string) and an employee number (type long). Include a member function
called getdata() to get data from the user for insertion into the object, and
another function called putdata() to display the data. Assume the name has
no embedded blanks. Write a main()program to exercise this class. It should
create an array of type employee, and then invite the user to input data for
up to 100 employees. Finally, it should print out the data for all the
employees.
Exercise3:
Write a program that calculates the average of up to 100 English distances
input by the user. Create an array of objects of the Distance class. To
calculate the average, you can create the add_dist() member function. You’ll
also need a member function that divides a Distance value by an integer.
7 P7: Learning Operator Overloading 3

Task:
 To study the conversion between objects and user-defined data types
 Experiments with C++ programs using unary and binary operator
overloading

Exercise1:

Create a class called Distance class, and add an overloaded - operator that
subtracts two distances. It should allow statements like dist3= dist1-dist2.
Assume that the operator will never be used to subtract a larger number
from a smaller one (that is, negative distances are not allowed).
Exercise2:
Create a class called time that has separate int member data for hours,
minutes, and seconds. One constructor should initialize this data to 0, and
another should initialize it to fixed values. The final member function
should add two objects of type time passed as arguments. A main() program
should create two initialized time objects and one that isn’t initialized. Then
it should add the two initialized values together, leaving the result in the
third time variable. Finally it should display the value of this third variable.
Write a program to test this class.
8 P8: Constructing Inheritance 5

Task:
 To study the concept of the inheritance
 To study the constructor rules for derived classes
 Experiments with reuse of the same class and code

Exercise1:

Create a class publication that stores the title (a string) and price (type float)
of a publication. From this class derive two classes: book, which adds a page
count (type int), and tape, which adds a playing time in minutes (type float).
Each of these three classes should have a getdata() function to get its data
from the user at the keyboard, and a putdata() function to display its data.
Write a main() program to test the book and tape classes by creating
instances of them, asking the user to fill in data with getdata(), and then
displaying the data with putdata().

Exercise2:

Start with the publication, book, and tape classes of Exercise1.


Add a base class sales that holds an array of three floats so that it can record
the dollar sales of a particular publication for the last three months. Include
a getdata() function to get three sales amounts from the user, and a putdata()
function to display the sales figures. Alter the book and tape classes so they
are derived from both publication and sales. An object of class book or tape
should input and output sales data along with its other data.
Write a main() function to create a book object and a tape object and
exercise their input/output capabilities.

Exercise3:

Create a base class, Person, which includes name, nrc, DOB, and show and
get functions.
Create a child class, Student, which includes rollno, paper1, paper2 and
functions such as show, get, calculateResult.
Create another child class, Teacher, which includes rank, dept and salary
and functions such as show, get, promote and raise salary.
Write a main() function and test the inheritance hierarchy.

Exercise4:

Create a class, Employee which has empno, rank, department, salary as


member data, and get and show as member functions.
Create a class, Student, which has prno, name, course as member data, and
get and show as member functions.
Create a class, CEIT-Student, which derives the above two classes and adds
“Search” function that can search student records by prno or course.
Write a main() function that gets 5 CEIT-Student record according to the
input value of prno or course.

You might also like