Anda di halaman 1dari 10

Course Code : CS 72

Course Title : C++ and Object Oriented Programming


Assignment Number : BCA (6)-72/Assignment/ 11
Maximum Marks : 100 (Weightage 25%)
Last Date of Submission : 30th April, 2011/30th October, 2011
www.bronzeacademy.com, info@bronzeacademy.com
CS-72

There are seven questions in this Assignment. Answer all the questions. You may use
illustrations and diagrams to enhance your explanations. Please go through the
guidelines regarding assignments given in the Programme Guide for the format of
presentation. Answer to each part of the question should be confined to about 300
words.

Question 1:

Explain the term Class and Object using the example of a student class. The student class
represents name, enrolment number, father’s name, telephone number, program code and
date of birth of a student. The class has a function to show the information about the student
except date of birth, and to modify the information of telephone number and program. List
the private and public members (data members as well as member functions) for the class
student. Define the data types of member variables using C++ and create few objects of the
class. Can you put information in those objects? Give reasons.
(8 Marks)
OBJECTS
Objects are the basic run-time entities in an object-oriented system. They may represent a
person, a place, a bank account, a table of data or any item that the program has to handle.
They may also represent user-defined data such as vectors, time and lists. Programming
problems is analyzed in terms of objects and the nature of communication between them.
Program objects should be chosen such that they match closely with the real-world objects.
Objects take up space in the memory and have an associated address like a record in Pascal,
or a structure in C.

When a program is executed, the objects interact by sending messages to one another. For
example, if “customer” and “count” are two objects in a program, then the customer object
may send a message to the account object requesting for the bank balance. Each object
contains data, and code to manipulate the data. Objects can interact without having to know
details of each other’s data or code. It is sufficient to know the type of message accepted, and
the type of response return by the objects. Although different authors represent them
differently, Fig shows two notations that are popularly used in object-oriented analysis and
design.

Object:STUDENT STUDENT
Total
DATA
Name
Date-of-birth Average
Marks
FUNCTIONS
Total Display
Average
Display
----------

www.bronzeacademy.com
info@bronzeacademy.com
CS-72

CLASSES
We just mentioned that objects contain data, and code to manipulate that data. The entire set
of data and code of an object can be made a user-defined data type with the help of a class In
fact, objects are variables of the type class. Once a class has been defined, we can create any
number of objects belonging to that class. Each object is associated with the data of type
class with which they are created. A class is thus a collection of objects of similar type. For
example, mango, apple and orange are members of the class fruit. Classes are user-defined
data types and behave like the built-in types of a programming language. The syntax used to
create an object is no different than the syntax used to create an integer object in C. if fruit
has been defined as a class, then the statement

Fruit mango;
Will create an object mango belonging to the class Fruit

Example
#include<iostream.h>
#include<conio.h>
#include<string.h>

class Student
{
private: //default
char *name;
long enrolment_number;
char *father_name;
long phone;
char *pro_code;
char *dob;
public:
Student(char *n,long e,char *f,long p,char *pc,char *d)
{
strcpy(name,n);
enrolment_number=e;
strcpy(father_name,f);
phone=p;
strcpy(pro_code,pc);
strcpy(dob,d);
}
void show()
{
cout<<"\nName\t\t"<<name;
cout<<"\nEnrolment\t"<<enrolment_number;
cout<<"\nFather\'s Name\t"<<father_name;
cout<<"\nPhone\t\t"<<phone;
cout<<"\nProgram Code\t"<<pro_code;
}
void change_phone(long new_phone)
{
phone=new_phone;
}

www.bronzeacademy.com
info@bronzeacademy.com
CS-72

};
void main()
{
clrscr();
Student x("Rahul",101,"Ram",234,"BCA","June 12");
x.change_phone(900);
x.show();
Student y("Rohit",102,"Hemant",434,"MCA","March 13");
getch();
}

Question 2:

Explain the following terms in the context of object oriented programming using the student
class of question 1. You must use C++ to define the student class. In addition, assume that the
student class has a sub class namely “PhD Student” who has an additional field
“Department”. The function to show the information about PhD student also prints the
department in addition to showing the information as shown by the student class.

(28 Marks)
(a) Overloaded Constructors

(b) Encapsulation
The wrapping up of data and functions into a single unit (called class) is known as
encapsulation. Data encapsulation is the most striking feature of a class. The data is not
accessible to the outside world, and only those functions which are wrapped in the class can
access it. These functions provide the interface between the object’s data and the program.
This insulation of the data from direct access by the program is called data hiding or
information hiding.

(c) Inheritance
Inheritance is the process by which objects of one class acquire the properties of objects of
another class. It supports the concept of hierarchical classification. For example, the bird
‘robin’ is a part of the class ‘flying bird’ which is again a part of the class ‘bird’. The
principle behind this sort of division is that each derived class shares common characteristics
with the class from which it is derived as illustrated in Fig.

In OOP, the concept of inheritance provides the idea of reusability. This means that we can
add additional features to an existing class without modifying it. This is possible by deriving
a new class from the existing one. The new class will have the combined features of both the
classes. The real appeal and power of the inheritance mechanism is that it allow the
programmer to reuse a class that is almost, but not exactly, what he wants, and to tailor the
class in such a way that it does not introduce any undesirable side-effects into the rest of the
classes.

Note that each sub-class defines only those features that are unique to it. Without the
use of classification, each class would have to explicitly include all of its features.

www.bronzeacademy.com
info@bronzeacademy.com
CS-72

Vehicles

Two wheeler Four wheeler

Personal Commercial
Bikes Scooters

(d) Polymorphism
Polymorphism is another important OOP concept. Polymorphism, a Greek term, means the
ability to take more than one form. An operation may exhibit different behaviours in different
instances. The behaviour depends upon the types of data used in the operation. For example,
consider the operation of addition. For two numbers, the operation will generate a sum. If the
operands are strings, then the operation would produce a third string by concatenation. The
process of making an operator to different behaviours in different instances is known as
operator overloading.

Figure illustrates that a single function name can be used to handle different number and
different types of arguments. This is something similar to a particular word having several
different meanings depending on the context. Using a single function name to perform
different types of tasks s known overloading.

Polymorphism plays an important role in allowing objects having different internal structures
to share the same external interface. This means that a general class of operations may be
accessed in the same manner even though specific actions associated with each operation
many differ. Polymorphism is extensively used in implementing inheritance.

Shape

Draw()

Circle object Box object Triangle object

Draw(circle) Draw(box) Draw(triangle)

Question 3:

What are the advantages of using UML? Create the class diagram for a University having
classes – Student, Teacher, Subject and Programme. Make suitable assumptions, if any.

(8 Marks)

www.bronzeacademy.com
info@bronzeacademy.com
CS-72

Question 4:

Explain the usage of the following C++ operators with the help of an example program.
(8 Marks)
(a) operator for type casting
C++ permits explicit type conversion of variables or expression using the type cast operator.

Traditional C casts are augmented in C++ by a function-call notation as a syntactic


alternative. The following two versions are equivalent:

(type-name) expression

Examples:

average = sum/float(i);

A type-name behaves as if it is a function for converting values to a designated type. The


function-call notation usually leads to simplest expressions. However, it can be used only if
the type is an identifier. For example,

p = int * (q);

(b) operator for bitwise OR


Bitwise OR operator which is represented as |. The rules that govern the value of the resulting
bit obtained after ORing of two bits is shown in the truth table below.
Condtion1 Condition2 Result (|)
0 0 0
0 1 1
1 0 1
1 1 1

(c) operator for dereferencing an address


The dereference operator * takes a pointer to a value (variable or object) and returns the
value. For example:-
Int_t my_int = 2;
Int_t *my_int_ptr = &my_int;
*my_int_ptr = 4;
my_int_ptr is a pointer to my_int. By dereferencing the pointer my_int is retrieved. In this
example it is then assigned to so my_int receives the value 4.

See operator precedence

www.bronzeacademy.com
info@bronzeacademy.com
CS-72

Other Uses for *

1. When * after a type name (either a basic data type or a class) it means the the type is a
pointer to that type, as shown in the above declaration of my_int_ptr. The logic is that
the declaration:-
2. Int_t *my_int_ptr;

states that *my_int_ptr is an Int_t, from which it follows that my_int_ptr must be a
pointer to Int_t

3. * is also an arithmetic operator.

(d) comma operator


In the C++ programming languages, the comma operator (represented by the token ,) is
a binary operator that evaluates its first operand and discards the result, and then
evaluates the second operand and returns this value (and type). The comma operator has
the lowest precedence of any C operator, and acts as a sequence point.
The use of the comma token as an operator is distinct from its use in function calls and
definitions, variable declarations, enum declarations, and similar constructs, where it acts
as a separator.

In this example, the differing behavior between the second and third lines is due to the
comma operator having lower precedence than assignment.
int a=1, b=2, c=3, i; // comma acts as separator in this line, not as an operator
i = (a, b); // stores b into i ... a=1, b=2, c=3, i=2
i = a, b; // stores a into i. Equivalent to (i = a), b; ... a=1, b=2, c=3, i=1
i = (a += 2, a + b); // increases a by 2, then stores a+b = 3+2 into i ... a=3, b=2, c=3, i=5
i = a += 2, a + b; // increases a by 2, then stores a = 5 into i ... a=5, b=2, c=3, i=5
i = a, b, c; // stores a into i ... a=5, b=2, c=3, i=5
i = (a, b, c); // stores c into i ... a=5, b=2, c=3, i=3
A handy reminder is that
(a, b)
has the same effect as using C's ternary operator like
(a ? b : b)
Because the comma operator discards its first operand, it is generally only useful where
the first operand has desirable side effects, such as in the initialiser or increment
statement of a for loop. For example, the following terse linked list cycle
detection algorithm (a version of Floyd's "tortoise and hare" algorithm):
bool loops(List *list)
{
List *tortoise, *hare; /* advance hare 2 times faster than tortoise */
for (tortoise = hare = list;

www.bronzeacademy.com
info@bronzeacademy.com
CS-72

hare && (hare = hare->next); /* tests for valid pointers + one step of hare */
tortoise = tortoise->next, hare = hare->next) /* comma separates hare and
tortoise step */
if (tortoise == hare) /* loop found */
return true;
return false;
}

Question 5:

Differentiate between call by value and call by reference with the help of an example in the
content of function call. Explain the advantages of call by value. What does C++ uses?
(8 Marks)
Function can accept any type of argument you want. You just need to give the argument a
name and a data type, just like you would a local variable. In fact, arguments are just local
variables that happen to be declared on the same line as the procedure name. You can use two
different methods for passing in parameters to procedure: call by value call by reference.
Ther’s a big difference between these two methods, as you’ll see in this section.

Recall that the parameters that appear in the definition of a function are called formal
parameters.

CALL BY VALUE
The call by value method copies the values of actual parameters into the formal parameters,
that is, the procedure crates its own copy of argument values and then uses them. To
understand this concept, let us consider one example. To test your grammar, your English
Teacher purposely writes grammatically incorrect passage on her sheet and gives it to you for
corrections. So you copy down the given passage on your own sheet and make corrections
there. This is an example of call by value method. Here, your teacher passes the sheet having
grammatically incorrect passage (which is the actual parameter here) to you, you copy the
passage on your sheet (this is the formal parameter). Whatever changes take place, are not
reflected back to the orginal as the value of original is copied onto another which is the work
coy. Thus, in call by value method, the changes are not reflected back to the original values.

In C++, to pass arguments by value, you don’t need to mention any keyword when declaring
arguments as shown below:

void byValFunction(int x){


:
:
}
Only a copy of a variable is passed when an argument is passed by value. If the procedure
changes the value, the change affects only the copy and not the variable itself.

CAL BY REFERENCE
In the call by reference method, the called procedure does not create its own copy of original
values, rather, it refers to the original values only by different names i.e. the references. Thus
the called procedure works with the original data and any change in the values gets reflected
to the data. To understand this concept, let us revise our same old example of grammatically

www.bronzeacademy.com
info@bronzeacademy.com
CS-72

incorrect passage. if your English teacher gives you the original sheet having the
grammatically incorrect passage and allows you to work upon the same sheet, then whatever
correction you make, will be there on the original. In other words, I can say the changes are
reflected back to the original as the value of the original is not copied anywhere rather
original itself has been made the work copy. Thus, in call by reference method, the original is
not copied anywhere rather original itself has been made the work copy. Thus, in call by
reference method, the changes are reflected back to the original values.

The call by reference method is useful in situation where the values of the original variables
are to be changed using a function.

(i) Mention *,& operator  in the argument definition.  

void byRefFunction(int *x)


{
}
If you specify a data type for an argument passed by reference, you must pass value
of that type for the argument. You can work around this by passing an expression, rather than
a data type, for an argument. C++ evaluates an expression and passes it as the required type if
it can.

Example
void refFun(int *x)
{
*x=100; //value at operator
}
void valFun(int x)
{
X=100;
}
void main()
{
int a=10,b=10;
refFun(a);
valFun(b);
cout<<a<<b;
}

Question 6:

Write a template class “binaryTree” in C++. The class should have functions for search and
insert and element into a binaryTree. Use this template to create a binary tree of integer keys.
Make suitable assumptions, if any.
(20 Marks)

Question 7:

Create a class AlphaString that stores only alphabets in a string of arbitrary length. The string
has a constructor that makes sure that String has only Alphabets. The class also has a copy
constructor, and an overloaded + operator. The overloaded + operator should perform the

www.bronzeacademy.com
info@bronzeacademy.com
CS-72

concatenation of two strings. Does the class need an explicit destructor? Justify your answer.

(20 Marks)

www.bronzeacademy.com
info@bronzeacademy.com

Anda mungkin juga menyukai