Anda di halaman 1dari 12

LAB MANUAL

CS102 Object Oriented Programming using C++

Implementation of C++ programming problems


Aim:

Main aim of this course is to understand and solve logical & mathematical problems through C++ language . Strengthen knowledge of a procedural programming language. Design and develop solutions to intermediate level problems using the C language. Further develop your skills in software development using a procedural language.

Objectives: The objective of this course is to offer the modern programming language C++ that shall help the students to implement the various concept of object orientation practically. The students will be able to programme in the object oriented technology with the usage of C++. This course will also prepare students with the necessary programming background for Data Structures using C/C++ and Java programming courses.

DESCRIPTION OF SCHEDULE:
Week Week 1 Practical List 1. Write a C++ program to display "Hello Computer" on the screen. 2. Write a C++ program to display Your Name, Address and City in different lines 3. Write a C++ program to find the area of a circle using the formula: Area = PI * r2 4. Write a C++ program to find the area and volume of sphere. Formulas are: Area = 4*PI*R*R Volume = 4/3*PI*R*R*R. 5. Write a C++ program to print the multiply value of two accepted numbers 6. Write a C++ program to convert centigrade into Fahrenheit. Formula: C= (F-32)/1.8. 7. Write an C++ program that declares 5 integers, determines and prints the largest

and smallest in the group. . 8. Write an C++ program that declares two integers, determines whether the first is a multiple of the second and print the result. [ Hint : Use the remainder operator.] 9. Write an C++ program that declares two integers, determines whether the first is a multiple of the second and print the result. [ Hint : Use the remainder operator.]

Week 2

Classes & Objects


Specifying a class, creating class objects, accessing class members, access specifiers: public, private, and protected, classes, objects and memory, static members, the const keyword and classes, the static objects, empty classes, nested classes, local classes, abstract classes

List of programs:
1. Write a program in C++ to display your name, Branch, Year on too the computer screen without using classes and object. All information should be displayed in the separate line. 2. Write a menu driven program in C++ to perform all basic arithmetic operation addition, subtraction, multiplication, and division of two given values. Program receives two values and required operation to be performed from the keyboard and display particular result of the required operation. 3. Write a menu driven program in C++ that receives 4 digit integer value the keyboard and perform following operations (a) Reverse of that no. (b) sum of number with its reverse (c). sum of alternative digits(1 digit+3 digit and 2 digit+4 digit) 4. Write a menu driven program in C++ to receive integer number and convert equivalent binary, octal, hexadecimal number. 5. Write a menu driven program in C++ to perform all basic arithmetic operation addition, subtraction, multiplication, and division of two given values using function and switch case. Program receives two values and required operation to be performed from the keyboard and display particular result of the required operation. 6. Write a program in C++ to display mark sheet. Of the student. Define a class that contains data members to store student information line name, Branch, semester, marks in 6 different subjects, etc. Declare some member functions to get these information from the key board, to calculate result and to display all gathered information on to the computer screen in proper format. 7. Define a class employee. Include the following members: Data Members: a. Name of the employee b. Age of the employee Member Functions: a. To get the name and age of the employee

b. To display the name and age of the employee. 9. Define a class BankAccount to represent a bank account. Include the Following members: Data Members: 1. Name of the depositor 2. Account Number 3. Type of account 4. Balance amount in the account Member Functions: 1. To assign initial value 2. To deposit an amount 3. To withdraw an amount after checking 10. Define a class employee having data members as emp_code, dept_code, age, basic, DA, HRA and three member functions as getdata(), putdata(), calculatesalary() to get, display all the values of data members and calculate the total salary by adding basic, DA, HRA. Write this program for 10(ten) employees using an array of objects. */
Week 3 BANKING OPERATION USING CLASS AND OBJECTS Algorithm Step 1: Create an object b of the class bank account. Step 2: Read the number of records. Step 3: call the init member function of the class bank account through the object created. Step 4: Assign 500 to the balance in the records. Step 5: Display a menu with the following options create deposit withdraw display. Step 6: Read the choice. Step 7: If the choice is create then call the create member function using the object. Step 8: prompt and read the users name, account number and account type for all the n records. Step 9: If the choice is deposit call the deposit member function using the function. Step 10: Read the account number. Step 11: Check whether this account number is already exiting in the list. If true Step 12: Read the amount to be deposited. Step 13: Update the balance by adding the amount deposited to the balance.

Step 14: Display the balance. Step 15: If the choice is withdraw, call the withdraw member function. Using object Step 16: Read the account number. Step 17: If the account number existing in the list then. Step 18: Read the amount to be withdrawn. Step 19: Retrieve the amount from balance. Step 20: If the balance is less than 500 then Step 21: Calculate balance + withdraw amount-500. Step 22: Assign 500 to the balance. Step 23: Display the withdrawn amount. Step 24: If the choice is display, invoke the display member function using the Object. Step 25: Read the account number. Step 26: If this account number is existing in the list then. Step 27: Display the details such as name, account number, account type and balance in the given account number. Week 4 INVENTORY OF BOOK USING CONSTRUCTOR AND DESTRUCTOR Algorithm Step 1: Create an object for the class book. Step 2: Declare the pointer variable for author, title, and publisher and the Variable for price and stock position. Step 3: Read the number of records. Step 4:Display a menu with the following choice create ,buybook, transaction and display. Step 5:Using switch case execute the statements corresponding to the choice. Step 6:If the choice is create, read the title, author, publishes, price and stock

position and pass it to the constructor of the book class. Step 7;Find the string length of each of the pointer variables dynamically. Step 8:If the choice is buy books, read the title, author, stock from the user and check these with the array already created. Step 9:If the author name and title matches then display the message:Available and read the number of copies. Step 10:Decrement the stock position by 1 and display the mount to be paid. Increment successful transaction by 1. Else display NOT success and increment the unsuccessful transaction by 1. Step 11:If the transaction; display unsuccessful transaction. the variables, successful transaction and

Step 12:If the choice is display, then display all the details such as title, author, price, publishes and stock position. Week 5

STUDENT EVALUTION USING INHERITANCE


Aim To write a C++ program for students evaluation concept using inheritance. Algorithm Step 1: create a class studentpersonal declare roll no,age,name,sex in protected mode. Step 2: Using a parameterized constructor initialize the values for all the data members. Step 3: Create another class studentmark that is inherited from the base class and having the data members mark1,mark2,mark3,mark4 Using a parameterized constructor initialize the value for mark1,mark2,mark3,mark4. Step 4: Create another class called studentsports and declare score as protected mode. Step 5: Create a class studentresult and public inherited form studentmarks and studentsports having the data members total,avg,grade.. Step 6: Calculate the total and avg and display the result. Week 6

1. Create a class called Employee that includes three pieces of information as instance
variables a first name (type String), a last name (type String) and a monthly salary

(double) 2. Create a constructor in above class to initialize the three instance variables. Provide

a get method for each instance variable.. 3. Create two employee objects and display each objects yearly salary. 4. Give each employee a 10% raise and display each Employees yearly salary again.. 5. Write C++ program to create five object of book, get information of book using getdata() function including name, price, publication and author. 6. Write search() function to search a specified book, if book is search return the
complete information of book and print the information of book using putdata()

function. Week 7

TO READ A VALUE OF DISTANCE FROM ONE OBJECT AND ADD WITH A VALUE IN ANOTHER OBJECT USING FRIEND FUNCTION
Aim To read a value of distance from one object and add with a value in another object using friend function. Algorithm Step 1 : Create two classes AB and AC and store the value of distances. Step 2: Declare friend function. Step 3: Read the value from the classes. Step 4: Perform addition to add one object of AB with another object of AC. Step 5: Display the result of addition. Week 8 Aim To create a C++ program for performing matrix operations using operator overloading. Algorithm Step 1: Create 3 object for the matrix class m1, m2, m3. Step 2: Read the elements of the matrix A and matrix B using the getdata function in the matrix class, which is invoked by the object m1 and m2. Step 3: Invoked the operator function for matrix addition using the statement m3=m1.operator+(m2). Step 4: Create an object c of the type matrix. Step 5: Calculate matrix addition of the matrices of m1 and m2 and MATRIX OPERATION USING OPERATOR OVERLOADING

Store it in the object c return c. Step 6: Display the resultant matrix using the display member Function of matrix by invoking through the object m3. Step 7: Call the operator function for matrix subtraction using the Statement m3=m1-m2. Step 8: Pass the object m1 and m2 to the operator function. Step 9: Create an object of type matrix. Step 10: Calculate matrix subtraction using the object m1 and m2 and store in the object created return c. Step 11: Invoke the display function using the object m3. Step 12: Call the operator function for matrix multiplication using the statement m3=m1*m2. Step 13: Create an object of type matrix. Step 14: Perform the matrix multiplication and store it in the object and return it. Step 15: Invoke the display function using the object m3. Week 9 AREA CALCULATION USING VIRTUAL FUNCTIONS Aim To create a C++ program for calculating the area using the virtual function. Algorithm Step 1: Create a class shape containing 2 members function and display area , which is virtual. Step 2: Create the object for the classes circle, rectangle and triangle. Step 3: Overload operator << to display the results of addition and subtraction operations on two polynomials. Step 4: Display the results. Week 10 Polymorphism Lab Objectives

To gain experience with describing the information contained within a class hierarchy the concept of a polymorphic variable and its relationship to class hierarchies the interaction between polymorphism and memory management how to distinguish between virtual and non-virtual overriding the concept of downcasting multiple inheritance designing and using software frameworks

Week 11

1. Write a C++ program in which you are overloading all arithmetic operators. 2. Overload [] operator. 3. Write a program that accepts two values either integer or double. Design functions that understand the input, add them and provide the correct output. 4. Create a base class called shape. Use this class to store two double type values that
could be used to compute the area of figures. Derive two specific classes called triangle and rectangle from the base shape. Add to base class, a member function get_data() to initialize base class data members and another member functions

display_area() to compute and display the area of figures. Mark the display_area()
as avirtual function and redefine this function in the derived class to suit their

requirments.(Use pure virtual function)

5. Write a program in C++ to demonstrate default constructor. Create a class


having two data members in the private section. Define a default constructor to initialize these data members to initial value and display these values with the help of member function 6. Write a program in C++ to demonstrate parameterized/constructor overloading constructor. Create a class calculator that contains four data members in it. Initialize data members with different values using parameterized constructor and perform various arithmetic operation over these values and display result on to the computer screen. 7. Create a class called Triangle that stores the length of the base and height of a right triangle in two private instance variables. Include a constructor that sets these values. Define two functions. The first is hypot( ), which returns the length of the hypotenuse. The second is area( ), which returns the area of the triangle. Week 12 1. Create a base class that contains a function display(), displaying I am in base . Function with same name display() is in derived class ,displaying I am in derive. 2. Write a C program that manipulates the above text file. The program must implements the operation to modify a record, delete a record and append new records. 3. Write C++ programs for the following operation to work like DOS Commands: a. create a test1.txt file b. copy test1.txt data into 2 nd file name test 2. txt c. compare test1.txt and test2.txt e. concatenate test1.txt and test2.txt into test3.txt and display the data

4. Write a C program to open two files containing integers (in sorted order) and merge their contents. Week 13

Operator Overloading and Type conversion


Defining operator overloading, rules for overloading operators, overloading of unary operators and various binary operators, overloading of new and delete operators, type conversion - basic type to class type, class type to basic type, class type to another class type.

List of programs:
1. Declare a class Number that contains two data member value1 and value2 of the type of integer, define constructor to give initial value, and perform addition , subtraction, multiplication and division of these two numbers using operating overloading of +,-,*,/ operator respectively [hint- binary
operator overloading using member function]

2. Declare a class Number1 that contains two data member value1 and value2 of the type of integer, define constructor to give initial value, and perform addition, subtraction, multiplication and division of these two numbers using operating overloading of +,-,*,/ operator respectively [hintbinary operator overloading using friend function]

3. Declare a class Number3 that contains a data member value of the type of integer, define constructor to give initial value, and perform unary minus ,increment and decrement this number using operating overloading of ,++,-- operator respectively [hint- Unary operator overloading using member function] 4. Declare a class Number3 that contains a data member value of the type of integer, define constructor to give initial value, and perform unary minus, increment and decrement this number using operating overloading of -,++,- operator respectively [hint- Unary operator overloading using friend function] 5. Define a class complex that contains two data member to store real and imaginary part of the complex number. Create a function to get values from the keyboard into these complex numbers, overload binary + and to calculate addition and subtraction of two complex numbers respectively using member function . 6. Write a program to demonstrate explicit type conversion from basic type to user defined data type. 7. Write a program to demonstrate explicit type conversion from User Defined data type to Basic data type data type. 8. Write a program to demonstrate explicit type conversion from one user defined data type to another user defined data type. 9. Write a program in C++ to calculate mean value of n numbers using friend function. 10. Write a program in C++ to find greater between two numbers using friend function. 11 Write a program in C++ to display students information using friend function. 12 Write a program in C++ to swap between two numbers using friend function.

Week 14

Inheritance
Introduction, defining derived classes, forms of inheritance, ambiguity in multiple and multi-path inheritance, virtual base class, overriding member functions, order of execution of constructors and destructors. Create a class A with some private data members and some public member function, now create a derived class B, that inherits A and having some data members and member functions its own, in main( ) function access attributes of base class with the help of derived class object to show inheritance concepts. 2. Create a class publication which has title of book and writers name. create another class sales which accounts no. of sales for every month (upto 3 months) and then calculate total sales. Write a program to solve the ambiguity problem in inheritance where two different classes are inherited from single base class and a new class is derived from these two derived classes. How this problem is solved with the help of virtual base class concept.

Week 15

Virtual functions & Polymorphism


Concept of binding - early binding and late binding, virtual functions, pure virtual functions, virtual destructors & polymorphism.

1. WAP to use this pointer to find elder from two person. Define a class Person to store age of the person. Define constructor/member function to give initial value to the data member age. And then define a function elder to compare ages of two different person using this pointer to find out the elder person. 2. WAP a program to show how member of the class are accessed through the pointer to class using arrow operator (->). 3. Write a program to show the concept of virtual function with the help of suitable programming example. 4. Create a simple shape hierarchy: a base class called Shape and derived classes called Circle, Square, and Triangle. In the base class, make a virtual function called draw( ),and override this in the derived classes. Make an array of pointers to Shape objects that you create on the heap (and thus perform upcasting of the pointers), and call draw( ) through the base-class pointers, to verify the behavior of the virtual function. If your debugger supports it, single-step through the code. 5. Modify Exercise 5 so draw( ) is a pure virtual function. Try creating an object of typeShape. Try to call the pure virtual function inside the constructor and see what happens. Leaving it as a pure virtual, give draw( ) a definition. 6. Write a small program to show the difference between calling a virtual function inside a normal member function and calling a virtual function

inside a constructor. The program should prove that the two calls produce different results.
Week 16

Exception handling
Aim of the program 1. Create a class with a main( ) that throws an object of class Exception inside a try block. Give the constructor for Exception a String argument. Catch the exception inside a catch clause and print the String argument. Add a finally clause and print a message to prove you were there. 2. Create your own exception class using the extends keyword. Write a constructor for this class that takes a String argument and stores it inside the object with a String reference. Write a method that prints out the stored String. Create a trycatch clause to exercise your new exception. 3. Write a class with a method that throws an exception of the type created in Exercise 2. Try compiling it without an exception specification to see what the compiler says. Add the appropriate exception specification. Try out your class and its exception inside a try-catch clause. 4. Define an object reference and initialize it to null. Try to call a method through this reference. Now wrap the code in a try-catch clause to catch the exception. 5. Create a class with two methods, f( ) and g( ). In g( ), throw an exception of a new type that you define. In f( ), call g( ), catch its exception and, in the catch clause, throw a different exception (of a second type that you define). Test your code in main( ).
6. Repeat the previous exercise, but inside the catch clause, wrap g( )s

exception in aRuntimeException.
Week 17

Learning Outcomes:
On completion of this module, the learner should be able to: Describe the syntax and semantics of the C++ programming language Explain the use of class and object. Work in a team to analyze engineering problems and develop C++ programs for solving these problems; Use the basic utilities and facilities for software development . debug and test software;

Develop a minor software in C++ language

Anda mungkin juga menyukai