Short Book of C++
Short Book of C++
Table of Contents
Identify the various real-world objects from the given scenario. Also, identify the
objects that depict Encapsulation, Abstraction, Inheritance, and Polymorphism..
Object-oriented Design
Sr. No. Assignment Question
Mary and Lisa are friends and also employees of the same company named Blue-
1
Designs Ltd. in New York, However, Lisa got transferred to a branch of the same
company in city Chicago. Now, Mary wants to send a bouquet to her friend Lisa on
her birthday. Since, Lisa works in another city far away from the city in which
Mary resides, Mary contacts a local flower shop and places an order with the
florist Rob. Rob in turn contacts the florist Jason who manages the branch of the
same shop in Lisa’s city. Rob gives Jason the details of the flowers to be used in
the bouquet and Lisa’s address. Upon reading the order, John realizes that he
currently does not have the roses required for making the bouquet. So he
contacts the wholesaler Steven to deliver some roses to him. Steven supplies the
roses to Jason. Now, Jason asks the flower arranger Bob to prepare the bouquet.
Bob prepares the bouquet and hands it over to the delivery boy with the address
of Lisa’s house. The delivery boy drives to Lisa’s house and delivers the bouquet.
From the given scenario, identify the various objects in the scenario, classify them
into appropriate categories or classes, and draw the class hierarchy. Also, identify
the agents in the scenario, the messages passed between them, and draw a
diagram to depict RDD.
public Book()
{
bookid = ”B000”;
bookname = ”abcde”;
author = ”unknown”;
}
Console.WriteLine(“Book Details”);
b1.showBook();
Console.WriteLine(“Publisher ”+ publisher);
}
}
Identify the various classes that can be created from the scenario and draw the
inheritance hierarchy to depict association between the classes. Also, list the type
of inheritance between the classes. Identify the various objects and classify them
into is-a and has-a abstraction models.
#include <iostream>
class Employee
{
protected:
int empId;
};
void displayDetails()
{
cout << “Employee Id is “+ empId << endl ;
cout << “Employee name is “+ empName << endl ;
cout << “Project title is “ + projTitle << endl;
cout << “Module title is “+ moduleTitle << endl;
}
};
Which type of inheritance has Mark used in the code? Identify and describe the
problem arising due to this type of inheritance and the solution provided in C++
to solve the problem. Apply the solution to the code and fix it.
Polymor phism
Sr. No. Assignment Question
1 Brian is a software developer in Focus-IT Ltd. He is currently working on School
management system. He has been assigned a task to create a class to display the
Student details of the different types of Students in the School as per
requirement. Also, he needs to create a general purpose class to store any type of
data as per requirement. Brian has written the following C# code by using
inheritance, overloading, overriding, polymorphic variable and generics:
class Student
{
int id;
string name;
Console.WriteLine(“6 hours”);
Console.WriteLine(“3 hours”);
}
}
Student s1;
s1 = new Student();
Student s2;
s2 = new PartTimeStudent();
s2.studyHours();
Overloading
Sr. No. Assignment Question
1 John is a developer at Shine Microsystems. He has been asked to develop a class
Inventory to store the various product details. The user should be able to set
values as per availability of the details. The details must be set along with the
creation of the object of the class. John has created the following Inventory class
in C# with its attributes:
class Inventory
{
string prodId;
string prodName;
int stock;
float price;
Inventory()
{
prodID=”P000”;
prodName=”Unknown”;
stock=0;
price=0.0;
}
The code created by John always initializes the attributes to the default values.
Which overloading technique should John use to achieve his objective? Apply the
technique and modify the code to allow the user to set the values of the attributes
as per availability of product details as soon as the object is created.
Over riding
Sr. No. Assignment Question
1 Roger is a student of Information Technology and is working on a project for
which he needs to create an Employee payroll system. He needs to identify the
types of employees and display the project details of the employee based on the
employee type. Roger has used the concept of overriding to achieve this. The C#
code written by Roger is as follows:
class Emp
{
public void display()
{
Console.WriteLine(“Project X”);
}
}
class FullTimeEmp:Emp
{
public void display(string projname) // method1
{
Console.WriteLine("Project "+ projname);
}
Form the given code, identify the display() method that depicts the concept of
Redefinition and the one that depicts the concept of Refinement. Also, predict the
output of the program.
class Book
{
public virtual void display()
{
Console.WriteLine("Any Book");
}
}
class Fiction : Book
{
public override void display()
{
Console.WriteLine("Book is a Fiction");
}
}
class Mystery : Book
{
public override void display()
{
Console.WriteLine("Book is a Mystery");
}
}
class ListBooks
{
public void ShowBook(Book b1)
{
Mystery m = (Mystery)b1;
m.display();
}
Generics
Sr. No. Assignment Question
1 Clark has written the following C# code by using generics to create a general purpose
class called Stack in which he can store any type of values.
public T Pop()
{
return t;
}
}
class RunStack
{
static void Main()
{
Stack s = new Stack();
s.Push(10);
s.Push(20);
int ans = s.Pop();
Console.WriteLine(“Popped “+number);
}
}
However, when he executes the code, it is giving compilation error that ‘The type
T could not be found’. Identify and explain the problem. Apply the solution and fix
the error in the code?
Identify the various softwares and tools that Steven can use to achieve this task.
Justify your answer by a detailed explanation for the use of a particular software
or tool.
Patter ns
Which patterns can Robin use to create the machine? List the component
interfaces and classes required to create the program. Create the class diagram of
the pattern depicting the component classes and their associations.
Table of Contents
Basics of C++
Assignment Question
As a part of the training program, you have to train the new joinees on the concept of
operator precedence. To explain the concept better, develop a program that demonstrates
the application of the precedence rules for the operators when multiple operators are
encountered in an expression. This program should demonstrate the application of
precedence for all the operators.
Assignment Question
The design team is debating on the use of the ‘if-else’ statements versus ‘switch’
statements. You have been assigned the task of preparing a comparison document between
nested ‘if-else’ and ‘switch’ statements. The document should highlight the advantages and
disadvantages of the two categories of statements. The document should also contain the
recommendation about the situations in which these statements should be used.
The size of each individual string data is not known. The team leader has asked you to
design a solution that allows the application to store string data without wasting memory
space. Develop a code snippet that demonstrates the method to store string data efficiently
eliminating wastage of memory space.
Function Overloading
Assignment Question
At present, the team is developing a drawing application for kids. The application draws
various shapes based on the inputs provided by the users. Assume that as part of the team,
you have developed a class Shape with a function Read that inputs the shape to be drawn
and a function Draw that draws the shape. The application should be capable of drawing
polygons, circles, and triangles.
Develop a C++ program for this requirement, which will also demonstrate the use of a
single function to draw any shape requested by the user.
Hint: Use function overloading to implement a function Draw with different parameters.
Inheritance
Assignment Question
Quest Software Solutions is one of the most popular companies in Toronto, Canada.
It has been contracted to develop an application for a college to manage the data of
students studying in the college. You, as a team member, are assigned a task of designing a
class structure to represent the information of the students and the courses in which they
are enrolled. Draw the class diagram that represents the design you have created to store
the information of the students and the courses they are enrolled in.
Write the C++ code snippet that represents the class declaration for the class diagram
created.
~~ End of Assignments ~~