0% found this document useful (0 votes)
31 views

Short Book of C++

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Short Book of C++

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Object-Oriented Programming Concepts and C++ Assignments

© Aptech Ltd. Version 1.0 Page 1


Object-Oriented Programming Concepts and C++ Assignments

Object-Oriented Programming Concepts

© Aptech Ltd. Version 1.0 Page 2


Object-Oriented Programming Concepts and C++ Assignments

Table of Contents

S# Session Page No.


1. Introduction to Object-oriented Programming 4
2. Object-oriented Design 5
3. Classes and Methods 6
4. Abstraction and Inheritance 8
5. Multiple Inheritance and Interfaces 9
6. Polymorphism 11
7. Overloading 13
8. Overriding 14
9. Polymorphic Variable 15
10. Generics 16
11. Frameworks and Reflection 17
12. Patterns 18

© Aptech Ltd. Version 1.0 Page 3


Object-Oriented Programming Concepts and C++ Assignments

Introduction to Object-oriented Programming


Sr. No.
John works as a project manager at Aaron-Tech Ltd. He is responsible for
1
managing the projects and documenting them. He keeps the confidential files of
all projects locked in a cupboard in his private cabin. Only John has access to the
cupboard keys. John works at the office from morning 9 am to evening 6 pm after
which he goes home. Today, John first went to the ATM to withdraw some money,
bought gifts for his family, and then went home. At home he spent quality time
with family and distributed the gifts to his father, wife, and his son respectively.

Identify the various real-world objects from the given scenario. Also, identify the
objects that depict Encapsulation, Abstraction, Inheritance, and Polymorphism..

© Aptech Ltd. Version 1.0 Page 4


Object-Oriented Programming Concepts and C++ Assignments

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.

© Aptech Ltd. Version 1.0 Page 5


Object-Oriented Programming Concepts and C++ Assignments

Classes and Methods


Sr. No. Assignment Question
1 Lisa is a student of computer science. She has been assigned a task to create a
program to show a class and the various class members. Lisa is going through a
reference book on object-oriented programming using C#. She has come across a
program that seems to meet the requirements of the task assigned to her. The
code is as follows:

public class Book


{
private string bookid ;
private string bookname;
private string author;
static string publisher;
private const string copyright=”Copyright@2010”;

public Book()
{
bookid = ”B000”;
bookname = ”abcde”;
author = ”unknown”;
}

public Book(string bid, string bname, string auth)


{
bookid = bid;
bookname = bname;
author = auth;
}

public string getBookid()


{
return bookid;
}

public void setBookid(string bid)


{
bookid=bid;
}

public string getBookname()


{
return bookname;
}

public void setBookname(string bname)


{
bookname=bname;
}

© Aptech Ltd. Version 1.0 Page 6


Object-Oriented Programming Concepts and C++ Assignments

public string getAuthor()


{
return author;
}

public void setAuthor(string auth)


{
author=auth;
}

public void showBook()


{
Console.WriteLine(“Book id ”+ bookid);
Console.WriteLine(“Book name ”+ bookname);
Console.WriteLine(“Author ”+ author);
Console.WriteLine(“Copyright ”+ copyright);
}
static void Main()
{
Book.publisher = “Think-Time Publications”;

Book b1 = new Book();


b1.setBookid(“B001”);
b1.setBookname(“OOP - The Easy Way”);
b1.setAuthor(“Harrison”);

Console.WriteLine(“Book Details”);
b1.showBook();
Console.WriteLine(“Publisher ”+ publisher);
}
}

From the given code, help Lisa to identify the following:


1. Class
2. Attributes
3. Access modifiers
4. Static data fields
5. Constant data fields
6. Constructors
7. Methods
8. Accessors
9. Mutators
10. Objects

Also, predict the output of the program.

© Aptech Ltd. Version 1.0 Page 7


Object-Oriented Programming Concepts and C++ Assignments

Abstraction and Inheritance


Sr. No. Assignment Question
1 Chris is the manager of the IT department in a technical institute named Instant
Learning Solutions in the city New York. The management of the institute has
decided to automate the various operations and transactions. Chris has been
assigned the task to create the system design of the institute. Chris studies the
system and comes to the conclusion that there are two kinds of employees in the
institute; Full-time and Part-time. Also, there are different designations such as
Accountant, Clerk, Counselor, Manager, HOD, and Faculty. There are
departments such as Front Office, Accounts, Counseling, Faculty Room and Class
room. The students are allowed to enroll for courses in the institute such as Java,
C#, UML, .NET and Tally. Chris will require creating association between all these
entities of the bank for the system to be automated.

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.

© Aptech Ltd. Version 1.0 Page 8


Object-Oriented Programming Concepts and C++ Assignments

Multiple Inheritance and Interfaces


Sr. No. Assignment Question
1 Mark is a programmer working at SoftSolutions Ltd. He has been assigned a
task to create an Employee Registration System in which a user can insert and
display details of the different types of employees working in the company. Mark
has written the following C++code to accomplish this task. However, upon
execution, the code is not giving the desired result:

#include <iostream>

class Employee
{
protected:
int empId;
};

class Manager : public Employee


{
protected:
string projTitle;
};

class Developer : public Employee


{
protected:
string moduleTitle;
};

class FullTimeEmp : public Manager, public Developer


{
public:
string empName;

void setDetails(int eid, string ename, string pTitle, string


mTitle)
{
empId= eid;
empName = ename;
projTitle = pTitle;
moduleTitle = mTitle;
}

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;
}

© Aptech Ltd. Version 1.0 Page 9


Object-Oriented Programming Concepts and C++ Assignments

};

static void Main()


{
FullTimeEmp ft;
ft.setDetails(100,”Roger”,”E-Gurushala”,”Enrollment”);
ft.displayDetails();
}

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.

© Aptech Ltd. Version 1.0 Page 10


Object-Oriented Programming Concepts and C++ Assignments

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;

void display(string name) // method1


{
// print name
}

void display(string name, int id) // method2


{
// print name and id
}

void display(int id, string name) // method3


{
// print id and name
}

int display(int id, string name) // method4


{
// print age and name
}

public virtual void studyHours() // method5

Console.WriteLine(“6 hours”);

class PartTimeStudent : Student

© Aptech Ltd. Version 1.0 Page 11


Object-Oriented Programming Concepts and C++ Assignments

public override void studyHours() // method6

Console.WriteLine(“3 hours”);

}
}

public class List<T>


{
public void Add(T n) // method7
{}

public T Remove() // method8


{}

static void Main()


{

Student s1;
s1 = new Student();

Student s2;
s2 = new PartTimeStudent();
s2.studyHours();

List<Student> stud = new List<Student>();


stud.Add(s1);
stud.Add(s2);
Student s3 = stud.Remove();
}
}

From the given code, identify the following:


1. Type of inheritance
2. Parent class
3. Child class
4. Overloaded methods
5. Incorrectly overloaded methods
6. Overridden methods
7. Generic class
8. Polymorphic variable

Also, predict the output of the program.

© Aptech Ltd. Version 1.0 Page 12


Object-Oriented Programming Concepts and C++ Assignments

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;
}

static void main()


{
Inventory inv = new Inventory();
}
}

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.

© Aptech Ltd. Version 1.0 Page 13


Object-Oriented Programming Concepts and C++ Assignments

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);
}

public override void display() // method2


{
base.display();
Console.WriteLine("Project Y");
}

static void Main()


{
FullTimeEmp emp = new FullTimeEmp();
emp.display();
emp.display("My Project");
}
}

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.

© Aptech Ltd. Version 1.0 Page 14


Object-Oriented Programming Concepts and C++ Assignments

Polymor phic Variable


Sr. No. Assignment Question
1 Maria is a student of Information Technology. She has designed a C# code to
print the detail of the book selected by the user. The code works fine whenever
user selects the book type Mystery. However, when user selects book type Fiction,
the code throws InvalidCastException. The code written by Maria is as follows:

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();
}

static void Main()


{
ListBooks lb = new ListBooks();
Book b = new Fiction();
lb.ShowBook(b);
}
}
Identify and explain the reason for the exception. What is the solution in C# for
this problem? Apply the solution and fix the problem in the code.

© Aptech Ltd. Version 1.0 Page 15


Object-Oriented Programming Concepts and C++ Assignments

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 class Stack<T>


{
private T t;

public void Push(T n)


{
t = n;
Console.WriteLine(“Pushed ”+t);
}

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?

© Aptech Ltd. Version 1.0 Page 16


Object-Oriented Programming Concepts and C++ Assignments

Frameworks and Reflection


Sr. No. Assignment Question
1 Steven is a software developer at Microtech Ltd. He has been assigned a task to
create a shopping portal with rich look and feel. He has to give good graphical
effects, a user friendly design, and provide navigation facility in the application.
Steven can use any programming language and any software development tool to
achieve this. The portal should also allow user to make queries, modify the
properties and values of the various controls in the application. The application
should react to user actions and display informative messages in response. The
application should have facility to store the data in different formats and upload
and download different types of files. The application should allow the
management to query the application to get details about the application itself
and print it in a report format.

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.

© Aptech Ltd. Version 1.0 Page 17


Object-Oriented Programming Concepts and C++ Assignments

Patter ns

Sr. No. Assignment Question


1 Robin is a project manager at Bright-Minds Ltd. He has been assigned a task to
create a customized pizza machine. The machine allows user to select the
toppings that he wants on his basic pizza. The user is allowed to select multiple
toppings such as olives, jalapeno, oregano, and capsicum. The toppings will be
sprinkled on the pizza in the sequence that they are selected. The toppings are
stored inside the machine and the user only has to select the desired topping by
punching the relevant buttons. The machine design should be simple and should
hide the complex process of setting, fetching, and sprinkling the topping from the
user.

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.

--- End of Assignments for Object-Oriented Programming Concepts ---

© Aptech Ltd. Version 1.0 Page 18


Object-Oriented Programming Concepts and C++ Assignments

Object-Oriented Programming with C++

© Aptech Ltd. Version 1.0 Page 19


Object-Oriented Programming Concepts and C++ Assignments

Table of Contents

S# Sessions Page No.


1. Object Oriented Concepts and C++ 21
2. Basics of C++ 22
3. Flow Control Statements 23
4. Functions, Pointers, and Arrays 24
5. Function Overloading 25
6. Inheritance 26
7. Multiple Inheritance and Polymorphism 27
8. Data Structures using C++ and Exception Handling 28

© Aptech Ltd. Version 1.0 Page 20


Object-Oriented Programming Concepts and C++ Assignments

Object Oriented Concepts and C++


Assignment Question

ABC Consulting Inc. a software development company in Atlanta is hired by Toursafe


Travel Corporation to develop an application for showcasing various tour packages on
offer. As a member of the development team, you are assigned a responsibility to select a
programming language to develop this application. You are asked to decide between using a
traditional procedure-based programming language and an Object Oriented Programming
language.

As a part of the task, conduct a research on various procedure based programming


languages and Object Oriented Languages and prepare a presentation that highlights the
advantages and disadvantages.

© Aptech Ltd. Version 1.0 Page 21


Object-Oriented Programming Concepts and C++ Assignments

Basics of C++

Assignment Question

Global Software Solutions is a software development company in Texas that is involved


in developing applications for various companies. The organization recently hired fresh
graduates as part of their team. You are assigned with the task of training them in
developing application using C++.

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.

© Aptech Ltd. Version 1.0 Page 22


Object-Oriented Programming Concepts and C++ Assignments

Flow Control Statements

Assignment Question

Lognum Systems is a software services company in Melbourne that is developing an


application for an electronics security company.

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.

© Aptech Ltd. Version 1.0 Page 23


Object-Oriented Programming Concepts and C++ Assignments

Functions, Pointers, and Arrays


Assignment Question

Arc Software Solutions based in Sydney, Australia, is developing an application in


which one of the functionalities is to store string data.

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.

© Aptech Ltd. Version 1.0 Page 24


Object-Oriented Programming Concepts and C++ Assignments

Function Overloading
Assignment Question

TechLead is a Phoenix based software development company that specializes in


developing applications using Object-Oriented programming languages such as C++.

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.

© Aptech Ltd. Version 1.0 Page 25


Object-Oriented Programming Concepts and C++ Assignments

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.

© Aptech Ltd. Version 1.0 Page 26


Object-Oriented Programming Concepts and C++ Assignments

Multiple Inheritance and Polymorphism


Assignment Question
Soft-tech Software Solutions in Phoenix is designing a software for a bank to manage
the accounts and the customers. The accounts are current, savings, and loan. Customers
are classified as retail and corporate customers. Write a code snippet that demonstrates use
of inheritance in definition of classes and polymorphism to perform operations on accounts
such as deposit, withdraw, check_balance, and so on.

© Aptech Ltd. Version 1.0 Page 27


Object-Oriented Programming Concepts and C++ Assignments

Data Structures Using C++ and Exception Handling


Assignment Question
Kazoo Software Solutions is developing a ticketing application that requires use of a
queue to keep track of requests from the users. Write a C++ program using a queue that
stores the request received from the customer. The program should perform tasks listed as
follows:
• Print the content of the queue
• Search for the element in the queue

~~ End of Assignments ~~

© Aptech Ltd. Version 1.0 Page 28

You might also like