














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Practice Exercise for object Oriented programming in C++. Solve the exercise and become Master in C++.
Typology: Exercises
1 / 22
This page cannot be seen from the preview
Don't miss anything!















Q. 1 Write a program that reads a number N and print all its divisors. Q. 2 Write a program for computing the difference of the digits of any given number Q. 3 Write a program to compute the sum of square of integers from 1 to 20. Q. 4 Write a program that takes a floating point number from user and prints an integral value after applying floor function. For example the if floating point number is from 1.1 to 1.9 then after applying ceiling function will be 1.
Pythagoras theorem to calculate third side. Output will be: Enter first side 3 Enter second side 4 The length of three sides is: First: 3 Second: 4 Third: 5 Q. 2 Fibonacci series is a series in which next number is obtained by adding two previous numbers. Write a program that prints the series up to the limit specified by the user. Q. 3 Write a program that take income of twelve months of year of a person from the user. A person earns different money each month. Add 12 months salary and print average salary in end Output will be: Enter first month income $ 10 . .
Enter Twelve month income $ 20 Average Salary of person 1 is 3. Repeat this process for 5 person. Q. 4 Write a program that take input an integer from user, calculate its factorial and then print its factorial. Q. 5 Write a program that prints sum of odd integers between 1 and 50. Q. 6 Write a program that takes two integers as input. The second number will be the power of first one. Output will be: Enter first integer 2 Enter power of this integer 3 The result is: 8 Q. 7 Take two numbers from the user and then divide the greater number by smaller one. Output will be: Divisor= Dividend= Remainder= Quotient= Repeat this process for at least one time up to any sentinel value. Q. 8 Take a number from user as the maximum upper limit and then print all those numbers from 2 to that limit that are prime.
Q. 1 Raising a number n to a power p is the same as multiplying n by itself p times. Write a function called power that take a double value for n and an int value for p, and return the double value. Write main function that gets value form user to test the function Q. 2 Write a functions rectarea to calculate the area of rectangle. It should take an argument of type float and return an argument of the same type. Write a main program that gets a base and height value form the user and call rectarea.
Q. 4 Write a recursive function power that calculates the power of given number. The data type for power/exponent will be integer and for base it will be double. The prototype for recursive function will be: double power (double base, int exponent); Write sample main function for calling your own defined function with base =2 and exponent = 4 For example cout << power (2, 4); will print 16. Q. 5 Write a function that receives a floating point number and returns an integral value after applying ceiling function. For example the if floating point number is from 1.1 to 1.9 then after applying ceiling function will be 2. Q. 6 Write a function findminimum that takes two parameters. The data type for both will be long. The function will return maximum of two values. Then write a main program that takes two values from user and passes these values to function findminimum and prints the maximum of two. Output will be: Enter first number 123 Enter second number 2 The minimum number is 2
Q. 1 Write a program that declares an array of size 10 in main. The array will be initialized by taking input from user. The values in array will be used to draw a histogram. For example if the values in array are like this: 0 1 2 3 4 5 6 7 8 9 3 5 10 7 6 2 0 4 13 8 The output will be:
**
Q.2 Write a function convertdec for converting a decimal number to binary number. The function will receive the number as parameter and will print its binary representation. The data type for the number will be long. User will input the number in main and will pass the number to function convertdec. Q. 9 Make a logical calculator that performs the following functionality
1 $ 2 $630 …… 3 $530 …… . 200 Total 2 1 miners Total $980 $ Salaries Average $490 $ Q. 4 Write a main that declares a string in c++. Then write a function that receives this string as pointer. The function will declare another string. Then take a string from user to
use any variable for traversing the array. Apply direct arithmetic on pointer variable. Then print the height for cities entered using for loop. For example height of city can be 2291m.
Q.1 Write a short program that will: a. Create structure to hold a data of a Employee including Name, CNIC, Age. b. In main create an array of 50 students and pass it in function studentinfo. c. Write a function studentinfo that receives an array of students of size 50 and that first takes input in this array and then print this information. The output will be: Enter name of student ABC Enter CNIC of student 35202 - 2514707 Enter Age of student 36 . . CNIC Name Age 35202 - 2514707 ABC 36 . . .
Q. 1 Write a program that prints the Counting 0 to 9 in the following format: 0 11 222 3333 Up to 9 , which is printed 9 times. Note: Use ASCII code for numbers.
nouns from user and returns their plurals on the basis of these rules: a. If noun ends in ” y„ remove ” y„ and add „ies.„ b. If noun ends in „s„, ” ch„, or ” sh„ and ” es.„ c. In all other case, just add ” s.„ The main will call the function and will display the plural of the noun. Note: The size of the string will be such that nouns of maximum length can be input. Output: Enter a noun: student
The plural of student is students
format. Use arrays for storing two matrix and resultant matrix. Also pointer notation should be used for all manipulation. Initialize two matrixes used in addition at the time of declaration.
a. Create structure to hold a data of a phone set including, inventory number that is time of entry of phone set in inventory, model number, color, brand name, and category. b. In main create an array of phone set of size 10 and pass it in function orderedinfo. c. Write a function orderedinfo that takes an array of phone set of size 10 and that first sorts on the basis of inventory number and then print the category and all other information of the phone set in the following form: Output: Catogory model_number Inventory_number color Brand_name Basic 1200 12:38 red Panasonic ….
return 1 otherwise 0. Iterate string as individual characters using loop. The ending condition will be null character.
Q. 1 Write a complete C++ program with two alternate functions specified below that each simply triple the variable count defined in main. Then compare and contrast the two approaches. These two functions are a) Function triplecallbyvalue that passes a copy of count call-by value, triples the copy, and returns value. b) Function triplebyreference that passes count with true call by reference via reference parameter, and triples the original copy of count through its alias Q. 2 Write a short program that will: d. Create structure to hold a data of a Person including Name, Age and Address e. Write a function personinfo that receive reference of one structure variable of type Person. It takes input for its members and displays it. f. In main call the function personinfo ( struct Person&). Q. 3 Write a function that calculates volume of box and returns it to calling program. The prototype of function is given below: int boxvolume (int length =1, int width =1, int height =1 ); a) Call this function using no argument, 1, 2,3 argument. b) Try for combination when you want to change only second argument. Q. 4 Raising a number n to a power p is the same as multiplying n by itself p times. Write a function namely power that take two arguments, the second argument will be int in all cases. Create a series of function with the same name that works for all types double, int, char, float long. Write a main() program that exercise these overloaded functions with all arguments.
Q. 1 Create a class called time. Its three members, all type int, should be called hours, minutes and seconds. It should also have an appropriate default constructor and following functions:
1. time ( int hrs, int min, int sec) Store these values in class data members. 2. time ( int sec) Convert and store it in class data members. Takes sec and covert it in the hours, minutes and seconds and then store it.
Q. 1 Create an labour class for labours in factory:
Member function Object non-const non-const non-const const const non-const const const
Q. 1 Create a class Complex to store and manipulate a Complex Number. It should be able to store Real and Imaginary portions as double , and provide appropriate functionality to correctly execute the following main( ). Comments at the end of each line refer to the Explanation Notes on next page. void main() { Complex c1( 3.0 ); //E Complex c2 = c1 + 4.5; //E02, E Complex c3; //E cin >> c3; //E c1 = c1 + 1.2 + c3 – (c1 – 7.5 – c3); //E05, E08, E cout << “The value of c1 is:” << c1 << endl; //E c2 = c3 = ((c1 > 20.5)? c1 : 20.5); //E05, E12, E cout << c2 << “, ” << c3; //E cin >> c1 >> c2 >> c3; //E if (c1 > c2 && c1 >= c3 || c2 == c3) //E12, E { Complex c4(3.5, 2.1); //E cout << c4 << endl; //E } else if (c2 == c3) //E { Complex c4 = c1 += c2 - = c3; //E02, E09, E cout << “Last value is:”<< c4 << endl; //E } else { c1 = - c1; //E c2 = ~c2; //E Complex c4 = c3++; //E cout << ++c4; //E } }
Explanation Notes: E00. Complex Number, in simple words, has two parts (1) Real and (2) Imaginary. You have to treat both the parts separately for the purpose of addition, subtraction etc. For example, 2+3 i is a Complex Number that has 2 as Real and 3 as Imaginary, whereas “ i ” is part of notation to write a Complex Number. In the Explanation Notes, wherever d is referred to as double value and c1 , c2 , c3 etc. are referred to as Complex value E01. Default Constructor: It would simply make both the parts zero. E02. Copy Constructor: It would perform a simple member wise copy. E03. Type Cast Constructor: It would assign the received value to Real part only, Imaginary will remain zero. E04. Parameterized Constructor: It would simply assign first parameter to Real and second to Imaginary part. E05. Assignment Operator E06. Output Operator: It should display the value in standard notation of Complex Number, i.e., 2+3 i. E07. Input Operator: It should prompt for each part of the value, e.g., Real: 2. Imaginary: 3654. E08. Plus Operator: It will not change the two operands rather it should return the result as Complex Number, e.g. c2 + c3 will give a Complex Number that has Real equals sum of Real parts of c2 and c3, similarly Imaginary equals sum of Imaginary parts of c2 and c3. Overload this operator for all the possibilities, like, c2 + c3, c2 + d, and d + c2. E09. Add Operator: It should be able to handle c1+=c2, c1+=d, and d+=c E10. Minus Operator: Same as Plus Operator, Real part of difference of operand1 and operand2 will be the difference of Real parts, and Imaginary part will be the difference of Imaginary part. Overload this operator for all the possibilities, like, c2 - c3, c2 - d, and d - c2. E11. Subtract Operator: It should be able to handle c1-=c2, c1-=d, and d-=c E12. Comparison (Relational/Equality) Operators: Real part of operand1 will be compared with Real part of operand2 and same with Imaginary part of both operands. However, Real part has precedence, e.g., 2+3 i is greater than 1+2543 i. E13. Negative Operator (Unary): It will change the sign of both parts (Real and Imaginary) E14. Complement Operator (Unary): It will swap both the parts with each other. E15. Post-Increment Operator (Unary): It will increment (by 1) the value of Real part only. E16. Pre-Increment Operator (Unary): It will increment (by 1) the value of Real part only. E99. No need to Overload Logical AND, Logical OR, and Conditional Operator. Q. 2 Write an Abstract Data Type (ADT) named CMatrix having following data members:
Q.1 Create a class called Student.
Q.1 Write the inheritance hierarchy of Community for university. Use the class Community to further specialization of university people. Derive two classes Employee and Student from class Community. Further derive two classes Faculty and Staff from class Employee. Write a class Administrator acting as Faculty and staff both. The four data members for class community are Name, Address, Age, and Id. The two data members for class student are Id and Salary. The three data members for class student are Id, Degree and CGPA. The only data member for Faculty is Qualification. Similarly the only data member for staff is Rank. Again the only data member for Administrator is Designation. For all classes provide appropriate constructor, destructor and wrapper functions.
Write a main in which a base class pointer will be used to call wrapper functions for every derived class from base class Community (Hint: Base class pointer is of Community.). Q.2 Create a base class Employee having three data members’ first name and last name (use character pointer for this) and earning. Then write derived classes of 1. Employee that are Boss who gets paid a fixed weekly salary regardless of numbers of hours worked.
//only function needed to set values is as under: void setnames (char *sname=”un-assigned”, char *fname=””, int id=0) { strcpy (fname,firstname); strcpy (sname,surname); this->id = id; }}; has been defined, and the random access file has been opened properly. Assume all header files are included. Do all the following tasks in main. a) Initialize the file”nameid.txt” with 20 records containing surname=“un-assigned”, firstname =””, and id = 0. b) Input surname, firstname and age of last 5 records out of 20 from user and write them to file. (Order should be maintained only last five records should be updated with new values.) c) Take an id from user which should be grater than 0. d) Update a record with new values that if id entered by user matches with any id already saved.
Q.1 Write code to execute the following code: void main () { power (5, 2); power(5.2, 4); power (40,000, 2); } Note: where first argument in this power function is the number that will be raised to value passed in second argument. Q. 2 Write an ADT namely Safearray that is preferable to built-in arrays in c++. Safearray will check the index each time the array is accessed to ensure it is not out of bounds. The size of the safe array will be given at the time of initialization. If not given by user some max value will be used to specify size. Provide the facility user can specify both the upper and lower bound of the array (indexes running from 100 to 200, for example). You will need to add two argument constructor that specifies the upper and lower bounds. Note: You can use mapping.
Q.3 Write down the class String for correct execution of the following main( ) : void main() { String s1('a'), s2("ABC"), s3=s2; String s4="abc", s5='A'; s2 = s5 = s4; cout << s2 << endl; cin >> s4; String s[3] = {“hufc”, “ hiit”, “ hims”}; for (int k =0; k<3; k++) cout << s[k] << endl; s2 = s5 + s4; s3 = s5 + ‘R’; s4 = s3 + ‘W’ + s4; s5 = “DD” + s4; s2 = s5 = ‘Y’; } Q.4 Fill in the blanks in each of the following : a) Members of the class specified as private members are accessible only to member functions of the class and __________________ of the class. b) The set of public member functions of the class is referred to as the class __________________. c) The default access for members of the class is __________________. d) The __________________ operator dynamically allocates memory for an object of a specified type and returns a pointer to that type. e) A __________________ data member represents class-wide information. f) The C++ operators that cannot be overloaded are __________________(any three). g) An object of a class can be treated as an object of its __________________ corresponding__________________ class. h) A “has a” relationship between classes represents __________________ and an” is a” relationship between classes represents __________________. i) The three member access specifies are __________________. j) If a class contains one or more pure virtual function, it is an _________________. k) A function call resolved at run time is referred to as ______________ binding. l) The stream manipulators ___________, ___________, and ___________ specify that integers should be displayed in octal, hexadecimal and decimal formats, respectively. m) The endl manipulator is used for __________________. n) The difference between function template and template function is __________________. o) __________________ are runtime errors. p) A stream is __________________ of bytes.