OOP Lab Manual-Spring-19 PDF

Title OOP Lab Manual-Spring-19
Course Object Oriented Programming
Institution Air University
Pages 26
File Size 1006 KB
File Type PDF
Total Downloads 9
Total Views 137

Summary

Download OOP Lab Manual-Spring-19 PDF


Description

Object Oriented Programming LAB MANUAL

Subject Code: CS-163 Class: BSCYS-II(A.B)

Prepared By Fatima Noor ul ain

Faculty of Computing and Artificial Intelligence Air University, Islamabad

Object Oriented Programming Lab Syllabus Recommended Systems/Software Requirements: Intel based desktop PC with minimum of 166 MHZ or faster processor with at least 64 MB RAM and 32GB free disk space. C++ compiler. S. No. List of Experiments 1 Write Programs to understand and implement the following a) What is a structure b) How the memory is allocation to a structure c) How to access the member variables d) How to initialize the member variables e) How to display the contents of the structure on the output screen 2 Write Programs to understand and implement the following

Page No. 5

8

a) b) c) d)

To introduce the concept of an abstract data type To introduce the concept of a structure To develop and manipulate an array of structures To use structures as parameters e) To use hierarchical (nested) structures 3

Write Programs to understand and implement the following a) To introduce object-oriented programming b) To introduce the concept of classes c) To introduce the concept of constructor Write Programs to understand and implement the following a) To introduce object-oriented programming b) To introduce the concept of classes c) To introduce the concept of constructors and destructors Write Programs to understand and implement the following a) Array of an object b) Objects as function parameters c) static data members d) const and classes e) friend functions & friend classes Write programs to understand and perform the following a) Member-wise Assignment b) Copy Constructor

10

7

Write programs to implement the following overloaded operators a) operator overloading with unary operator b) operator overloading with binary operators c) operator overloading with stream insertion and extraction operators

16

8

Write programs to implement inheritance a) Base Class b) Child Class c) Calling Base Constructors d) Function Overriding

18

9

Write programs to implement following inheritance types a) Multilevel inheritance b) Multiple inheritance

21

10

Write Programs to understand pointers a) pointer to object

23

4

5

6

11

13

15

b) pointer to array c)

this pointer - Every object in C++ has access to its own address through an important pointer called this pointer which points to the object itself.

11 Write programs to understand aggregation

Object Oriented Programming LABORATORY

26

Course synopsis This course introduces the concepts of object-oriented programming to students with a background in the procedural paradigm. The course begins with a brief review of control structures and data types with emphasis on structured data types and array processing. It then moves on to introduce the objectoriented programming paradigm, focusing on the definition and use of classes along with the fundamentals of object-oriented design. Other topics include inheritance, polymorphism, operator overloading, exception handling, templates and STLs. Students will learn about these concepts in a C++ development environment.

Objective This course provides in-depth coverage of object-oriented programming principles and techniques using C++. Topics include classes, overloading, data abstraction, information hiding, encapsulation, inheritance, polymorphism, file processing, templates, exceptions, container classes, and low-level language features

Learning Outcomes: At the end of the course, students will be able to:      

Describe Object Oriented Programming Tackle real world problems through Object Oriented Programming Handle Operator overloading Implement Inheritance, Polymorphism, Encapsulation and related features of OOP Handle Constructors & Destructors Implement Generic Programming Concepts and manipulate exception handling

Week-1

1.1 OBJECTIVE 1. To create an Array of an object 2. Making use of Objects as function parameters 3. Making use of static data members 4. To create a constant data member and classes 5. To create friend functions & friend classes 1.2 RESOURCE: Visual Studio  C++ 1.3 PROCEDURE Go to Build -> run or press F5 to run the program Experiment: 1 Write a program that declares a structure based on the following entity shown in the box. The program must declare and initialize an instance of the structure entity i.e. Student. The attributes of the entity are shown in oval shapes associated with the entity. Use the following data type for the attributes of the student:  integer for studentID   char[] for studentName   char [] for courscode   char[] for courseName   integer for courseMarks Please the following data to initialize the student instance:

Experiment: 2 Create a structure of type Date that contains three members: the month, the day of the month, and the year, all of type int. (Or use day-month-year order if you prefer.) Have the user enter a date in the format 12/31/2001, store it in a variable of type struct Date, then retrieve the values from the variable and print them out in the same format. Experiment: 3 Create a structure called Employee that contains two members: an employee number (type int) and the employee’s compensation (in dollars; type float). Ask the user to fill in this data for three employees, store it in three variables of type struct Employee, and then display the information for each employee. Use a seperate display function for the information display. Experiment: 4 Write a program that declares a structure based on the in the figure in exercise 01. This time you will declare three instances of the entity student and shall initialize the three instances with the data shown in the table below. You have to find the total marks of the students in all of the student instances. 5

Within the body of this main function do the following   

 





Declare three instances of the student structure student st1, st2, st3, st4; Initialize each of the instance with the data shown in the table Access the courseMarks of each instance step by step and add them all together and save the result in a new integar named totalMarks. Display the total marks of the students at the end of the program.

6

Week-2 2.1 OBJECTIVE 1. To introduce the concept of an abstract data type 2. To introduce the concept of a structure 3. To develop and manipulate an array of structures 4. To use structures as parameters 5. To use hierarchical (nested) structures 2.2 RESOURCE: Visual Studio  C++ 2.3 PROCEDURE Go to Build -> run or press F5 to run the program

Experiment: 1 Write a program to define a structure with 5 members. The first member be student name and the other member be student roll number and marks obtained in 3 subjects. Input values from the user. Add the marks of the subject and calculate the total marks and then print these numbers and total marks of the student. Experiment: 2 Create a structure of type Date that contains three members: the month, the day of the month, and the year, all of type int. (Or use day-month-year order if you prefer.) Have the user enter a date in the format 12/31/2001, store it in a variable of type struct Date, then retrieve the values from the variable and print them out in the same format. Experiment: 3 Create a structure called Employee that contains two members: an employee number (type int) and the employee’s salary (in dollars; type float). Ask the user to fill in this data for three employees and then display the information for each employee. Use a separate display function for the information display. Experiment: 4 Write a program that uses a structure named MovieData to store the following information about a movie:  Title   Director   Year Released   Running Time (in minutes) The program should create two MovieData variables, store values in their members, and pass each one, in turn, to a function that displays the information about the movie in a clearly formatted manner.

7

Experiment: 5 Suppose you have been provided with two data type i.e. one for the distance measurement and the other as triangle. Your target is to find the area of the triangle using two structures one for the distance having feet and inches as members and other with triangle having base and height as members. The base and height both will be measures in inches and feet. The attributes of both are shown below: Note: integer for feet, float for inches the data type of both base and height will be the Distance. The formula to find the area of triangle is: area = ½ base * height. Experiment: 6 Calculate the wage of an employee who works five times a week. He gets his wages on per hour basis. A week has five working days according to which his daily work hours have been counted on per hour basis. His wage rate is 11 euro per hour. Please find the total wage of the employee if he works according to the following hours as shown in the table 01. The figure below shows the basic structures of the main data types. Use structural approach to find the solution of this problem.

8

Week-3 3.1 OBJECTIVE 1. To introduce object-oriented programming 2. To introduce the concept of classes 3. To introduce the concept of constructor 3.2 RESOURCE: Visual Studio  C++ 3.3 PROCEDURE Go to Build -> run or press F5 to run the program Experiment: 1 Design a class called Date. The class should store a date in three integers: month, day, and year. There should be member functions to print the date in the following forms: 12/25/2014 December 25, 2014 25 December 2014 Demonstrate the class by writing a complete program implementing it. Input Validation: Do not accept values for the day greater than 31 or less than 1. Do not accept values for the month greater than 12 or less than 1.

Experiment: 2 Write a class definition Distance with two data members feet and inches This class also contains member functions 





void setFeet(int)



void setInches( float)



void showDistance()

Demonstrate the class by writing a complete program implementing it.

Experiment: 3 Write a class named Employee that has the following member variables:

  



Name: A string that holds the employee’s name



ID Number: An int variable that holds the employee’s ID number



Department: A string that holds the name of the department where the employee works



Position: A string that holds the employee’s job title

9

Write appropriate mutator functions that store values in these member variables and accessor functions that return the values in these member variables. Once you have written the class, write a separate program that creates three Employee objects to hold the following data. The program should store this data in the three objects and then display the data for each employee on the screen.

Experiment: 4 Write a class named Car that has the following member variables:

 





yearModel: An int that holds the car’s year model



Company: A string that holds the maker of the car



Speed: An int that holds the car’s current speed 

Mutator: Appropriate mutator functions to set the values of data members



Accessor: Appropriate accessor functions to get the values stored in an object’s yearModel, company, and speed member variables

 

Accelerate: The accelerate function should add 5 to the speed member variable each time it is called

 

Brake: The brake function should subtract 5 from the speed member variable each time it is called

Demonstrate the class in a program that creates a Car object, and then calls the accelerate function five times. After each call to the accelerate function, get the current speed of the car and display it. Then, call the brake function five times. After each call to the brake function, get the current speed of the car and display it.

Experiment: 5 Write a class that displays a simple message on the screen whenever an object of that class is created.

10

Week-4 4.1 OBJECTIVE 1. To introduce object-oriented programming 2. To introduce the concept of classes 3. To introduce the concept of constructors and destructors 4.2 RESOURCE: Visual Studio  C++ 4.3 PROCEDURE

Go to Build -> run or press F5 to run the program Experiment: 1   

Write a class named Employee that has the following member variables:  Name: A string that holds the employee’s name  idNumber: An int variable that holds the employee’s ID number  Department: A string that holds the name of the department where the employee works  Position: A string that holds the employee’s job title A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee’s name, employee’s ID number, department, and position  A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee’s name and ID number. The department and position fields should be assigned an empty string ("")  A default constructor that assigns empty strings ("") to the name, department, and position member variables, and 0 to the idNumber member variable Write appropriate setter/getter functions that store values and return the values in these member variables. Once you have written the class, write a program that creates three Employee objects to hold the following data. The program should store this data in the three objects and then display the data for each employee on the screen.

 



Experiment: 2         

Write a Circle class that has the following member variables.  radius: a double  pi: a double initialized with the value 3.14159 The class should have the following member functions: Default Constructor: A default constructor that sets radius to 0.0 Constructor: Accepts the radius of the circle as an argument  setRadius: A mutator function for the radius variable  getRadius: An accessor function for the radius variable  getArea: Returns the area of the circle, which is calculated as: area = pi * radius * radius  getDiameter: Returns the diameter of the circle, which is calculated as: diameter = radius * 2  getCircumference: Returns the circumference of the circle, which is calculated as: 11

circumference = 2 * pi * radius Write a program that demonstrates the Circle class by asking the user for the circle’s radius, creating a Circle object, and then reporting the circle’s area, diameter, and circumference.

Experiment: 3 Design an Inventory class that can hold information and calculate data for items in a retail store’s inventory. The class should have the following private member variables:

Demonstrate the class in a driver program. Input Validation: Do not accept negative values for item number, quantity, or cost.

12

Week-5 5.1 OBJECTIVE 3. To create an Array of an object 4. Making use of Objects as function parameters 3. Making use of static data members 4. To create a constant data member and classes 5. To create friend functions & friend classes 5.2 RESOURCE: Visual Studio  C++ 5.3 PROCEDURE Go to Build -> run or press F5 to run the program. Experiment: 1 Design a Payroll class that has data members for an employee’s hourly pay rate, number of hours worked, and total pay for the week. Write a program with an array of seven Payroll objects. The program should ask the user for the number of hours each employee has worked and will then display the amount of gross pay each has earned. Input Validation: Do not accept values greater than 60 for the number of hours worked. Experiment: 2 The objective of this exercise is to understand the declaration of a class with its data members and Member functions. Also, the concept of passing objects as function parameters. Create a class Travel with two data members kilometer and hour, class member functions input, show and add, which accept an object of Travel as parameter. It adds the values of data members of the parameter object and the value of the calling object’s data members and displays the result. Declare two objects t1 and t2 of class Travel and input data in both objects. Call the add function with t1 object and pass t2 object as an argument. Travel km : int hr : int Travel() ~Travel() input() : void show() : void Add(Travel p) : void Experiment: 3 The objective of this exercise is to understand the concept of static data members. Write a class definition Capital with one static data member count, which count number of objects created of particular class. A UML diagram of this class is given as: Capital count : int static Capital() show() void

:

13

Define variable count and initialize it to 0. This variable will be defined outside the class because it will be not part of any object. It is created only once in the memory and shared among all objects of the class. The variable definition outside the class must be proceeded with class name and scope resolution operator :: The compiler does not display an error of the static data member is not defined. The linker will generate an error when the program is executed. Create three objects x, y and z of Capital class. Each time an object is created, the constructor is executed that increases the value of the static count variable by 1.

Experiment: 4 The objective of this exercise is to understand the concept of const object type arguments and arguments passing by reference. Problem Statement Write a class definition ComplexNumber with two data members’ realPart and imaginaryPart. We wish to perform addition, subtraction and multiplication with objects of ComplexNumber class. A complex number c = a + bj consists of two parts, the real part a and the imaginary part bj. We can perform arithmetic operations (+, -, *, /, or addition, subtraction, multiplication) on complex numbers as follows: +: (a1 + b1j) + ( a2 + b2j) = (a1 + a2) + (b1 + b2)j -: (a1 + b1j) – (a2 + b2j) = (a1 – a2) + (b1 – b2)j *: (a1 + b1j) * (a2 + b2j) = (a1a2 – b1b2) + (a1b2 + a2b1) j A UML diagram of this class is given as: ComplexNumber realPart : double imaginaryPart : double Complex () ~Complex () Complex (double, double) add (const Complex&) : Complex sub (const Complex& ) : Complex mul(const Complex& ) : Complex print() : void const

Create objects of this class and test all the member functions. Experiment: 5 The objective of this exercise is to how to learn the friend function of a class. Room width : double printWidth(Room) : friend void setWidth(double) : void Procedure Do the exercise and create an object of the Room class and call the setWidth() function to pass 10.10 as the argument value. Later print the width of the room by calling the printWidth(Room) function. But see this function is a friend of the Room class, so this will not be a member of the class. Therefore we shall define it outside the class, rather than using the :: operators with the class member function definition.

14

Week-6 6.1 OBJECTIVE 1. Member-wise Assignment 2. Copy Constructor 6.2 RESOURCE: Visual Studio  C++ 6.3 PROCEDURE Go to Build -> run or press F5 to run the program.

Experiment: 1

Create a class ...


Similar Free PDFs