0% found this document useful (0 votes)
65 views8 pages

C++ Assignment

This document outlines 3 assignments for students on C++ concepts. Assignment 1 (Ch. 2) contains 11 questions on classes, access specifiers, encapsulation, functions, and friend functions. Assignment 2 (Ch. 3) has 12 questions on constructors, destructors, copy constructors, and dynamic memory allocation. Assignment 3 (Ch. 4) provides 10 questions on inheritance, visibility modes, ambiguity resolution, and composition vs inheritance. It also lists 7 programming problems applying these concepts. Students must answer the questions for each assignment and complete the programs to earn up to 6 total marks.

Uploaded by

GSaurav Dahal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
65 views8 pages

C++ Assignment

This document outlines 3 assignments for students on C++ concepts. Assignment 1 (Ch. 2) contains 11 questions on classes, access specifiers, encapsulation, functions, and friend functions. Assignment 2 (Ch. 3) has 12 questions on constructors, destructors, copy constructors, and dynamic memory allocation. Assignment 3 (Ch. 4) provides 10 questions on inheritance, visibility modes, ambiguity resolution, and composition vs inheritance. It also lists 7 programming problems applying these concepts. Students must answer the questions for each assignment and complete the programs to earn up to 6 total marks.

Uploaded by

GSaurav Dahal
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

Dear students, find out your assignment 1,2 and 3.

This chapter includes chapter 2,3


and 4. Each assignment includes 2 marks i.e (total 6 marks) .Student must be able to
answer the question regarding assignment they have submitted. If you have any
queries regarding assignment or any subject matter feel free to ask me.

Assignment -1

(2 marks)
Chapter-2
Theory

1. Differentiate between class and structure. Explain them with example.


What sorts of shortcomings of structure are addressed by classes? Explain giving
appropriate example.
2. Explain the various access specifiers used in C++ with an example.
3. What is data hiding? How do you achieve data hiding in C++? Explain with suitable
program.
4. What is encapsulation? How can encapsulation enforced in C++? Explain with suitable
example code.
5. What are the common typed of function available in C++? Define the 3 common types of
functions in C++ with a program.
6. Does friend function violate the data hiding? Explain briefly.
7. “Friend function breaches the encapsulation.” Justify. Also mention the use of friend
function. List out merits and demerits of friend function.
8. Private data and function of a class cannot be accessed from outside function. Explain
how is it possible to access them with reference of an example.
9. What is inline function? Explain its importance with the help of example program.
10. What are the static data member and static member functions? Show their significance
giving examples.
11. Write short notes on:
 Reference variable
 Default argument

Program
1. Define a class to represent a bank account .Include the following

Data members.
 Name of the depositor
 Account number
 Type of account
 Balance amount in the account
Member functions
 To assign initial values
 To deposit an amount
 To withdraw an amount after checking the balance
 To display name and balance.
Write a main program to test the program.

2. Create a class Employee with data members (id, name, Post, address, salary) and read
information of 20 Employees and display the name and post of employee whose salary
is greater than 50000.
3. Using class write a program that receives inputs principle amount, time and rate.
Keeping rate 12% as the default argument, calculate simple interest for five
customers.
4. WAP to perform addition of two times using friend function.
5. WAP to perform addition of two complex numbers using friend function.
6. WAP to swap contents of two variables of 2 different classes using friend function.
7. Create classes called Vechile1, Vechile2 and Vechile3 with each of having one private
member named price. Add member function to set a price(say setPrice()) one each
class. Add one more function max() that is friendly to all classes. max() function should
compare private member named price of three classes and show maximum among
them. Create one-one object of each class and then set a value on them. Display the
maximum price among them.
Assignment -2

(2 marks)

Chapter-3
1. What is message passing? Describe with example.
2. Differentiate message passing and procedure call with suitable example. What are
the possible memory errors in programming.
3. Explain message passing formalism with syntax in C++.What is stack Vs heap
memory allocation?
4. “A constructor is a special member function that automatically initializes the objects
of its class”, support this statement with a program of all types of constructors. Also
enlist the characters of constructors.
5. What is constructor? Is it mandatory to use constructor in class. Explain.
6. What is copy constructor in C++? Is it possible to pass object as argument in Copy
constructor? Explain with suitable program.
7. Differentiate between constructor and destructor. Can there be more than one
destructor in a program for destroying a same object. Illustrate you
answer.
8. What are constructors and destructors? Explain their types and uses with good
illustrative example? What difference would be experienced if the features of
constructors and destructors were not available in C++.
9. What is de-constructor? can you have two destructors in a class? Give example to
support your reason.
10. Discuss the various situations when a Copy constructor is automatically invoked.
How a default constructor can be equivalent to a constructor having default arguments.
11. What do you mean by dynamic constructor? Explain its application by a program to
compute complex numbers.
12. What is dynamic memory allocation? How memory is allocated and de-allocated in
C++? Explain with examples. List out the advantages of dynamic memory allocation?
Explain with suitable example.

Programs

1. Write a Program to add two complex number using the concept of


Constructor.
2. Create a class Mountain with data members name, height, location, a constructor
that initializes the members to the values passed it to its parameters, a function called
CmpHeight() to compare two objects and DispInf() to display the information of
mountain. In main create two objects of the class mountain and print the information of
the mountain which is greatest height.
3. Create a class time constructor having hour, minute and second as an arguments is use
to take two time data from user. The add function that takes two class objects an
arguments add them respectively then display the aggregate result?
(Apply 60 second =1 minutes and 60 minutes =1 hour)
4. Create a class named Height with data members feet and inches. In main function create two
objects of class Height. Initialize one object using parametrized constructor and copy this values
to another object. Now finally perform the addition between these two objects.
5. WAP to add two distances given in km and m using dynamic constructor.
6. WAP to input marks of 35 students and find the highest marks using Dynamic memory
allocation.

Assignment -3

(2 marks)

Chapter-4
1. “Inheritance allows us to create a hierarchy of classes. Justify this statement. Discuss
private and public inheritance.
2. How does visibility mode control the access of members in the derived class? Explain
with an example.
3. Why protected access specifier used in C++? Explain different types of inheritance with
examples.
4. How does inheritance support reusability? What are the different forms of inheritance?
Explain with example.
5. When base class and derived class have the same function name what happens when
derived class object calls the function?
6. Inheritance supports characteristic of OOP. Justify your answer. Explain ambiguity that
occurs in multiple inheritance.
7. During the time of hybrid inheritance when there is hierarchical inheritance at the upper
level and multiple inheritance at lower level, ambiguity occurs due to the duplication of
data from multiple path at the grand child class. How this kind of ambiguity is resolved?
Explain with suitable example?
8. How are arguments are sent to base constructors in multiple inheritance? Who is
responsibility of it
9. How does inheritance influence working of constructors and destructors? Class ‘Y’ has
been derived from class ‘X’ .The class ‘Y’ does not contain any data members of its own.
Does the class ‘Y’ require constructors? If yes why.
10. What is containership? How does it differ from inheritance, describe how an object of a
class that contain object of another classes are created.
How composition differs from inheritance?
11. Explain how composition provide reusability?
12. Distinguish between subclass and subtype in light of principle of substitutability.
Support your answer with suitable example.

Differentiate between

 subclass and subtype.


 Is a rule and has a rule
 Software reusability
 Generalization

Programs
1) WAP to enter information of n Employee and then display is using the concept multiple
inheritance.
2) Write a complete program with reference to the given figure.

3) The following figure shows the minimum information required for each class. Write a
progam by realizing the necessary member functions to read and display information of
individual object. Every class should contain at least one constructor and should be
inherited from other classes as well.
4) The following figure shows the minimum information required for each class. Write a
program to realize the above program with necessary member functions to create the
database and retrieve individual information .Every class should contain at least one
constructor and should be inherited to other classes as well.

5) Create a class called Person with suitable data members to represent their name and
age. Use member function to initialize and display these information. Derive Student
and Employee from the Person class with their unique features. Initialize object of these
class using constructor and display the information.
6) Write a program to input two vector coordinates from the base class named “Base”.
Class “Derived” inherits all the properties of class “Base”. Class “Derived” must contain a
function named add_vector() that add the two vectors input from the base class and
finally display the result from the function display() that is friend to the base class.
7) Create a class student with two data members represent name and age. Use appropriate
member function to read and print these data members name and roll. Derive a class marks
from student that has additional data member sessional1,sessional2 to store sessional
marks. Derive another class result from marks and add the sessional marks. Use appropriate
member function to read and display data in the class.
8) Create a class student with two data members to represent name and age. Use member
function to read and print those data. From this derive a class called boarder with member
data to represent room number .Derive another class called day-scholar from class student
with member data to represent address of student. In both derived class use function to
read and print respective data.
9)The following figure shows minimum information required for each class.

i) Write a Program to realize the above program with necessary member functions to
create the database and retrieve individual information
ii) Rewrite the above program using constructor on each class to initialize the data
members.
10) Write a program that allow you to book a ticket for person and use two classes Person,
Reservation. Class Reservation is composite class/ container class.
11) Develop a complete program for an institution, which wishes to maintain a database of its
staff. The database is divided into number of classes whose hierarchical relationship is shown in
the following diagram. specify all classes and define constructors and functions to create
database and retrieve the individual information as per requirements.

12) Develop a complete program for an institution which wishes to maintain a database of its
staff. Declare a base class STAFF which include staff_id and name. Now develop a records for
the following staffs with the given information below.
Lecturer (subject, department)
Administrative staff (Post, department)

You might also like