You are on page 1of 9

Spring 2012

Master of Computer Application (MCA) Semester 2 MC0066 OOPS using C++ 4 Credits
(Book ID: B0681 & B0715) Assignment Set 1 (40 Marks)
Questions from Book-ID: B0681

1. Distinguish between procedural language and OOP language. And explain the key features of OOP. Ans: The programming languages have evolved from machine languages, assembly languages to

high level languages to the current age of programming tools. While machine level language and assembly language was difficult to learn for a layman, high level languages like C, Basic, Fortran and the like was easy to learn with more English like keywords. These languages were also known as procedural languages as each and every statement in the program had to be specified to instruct the computer to do a specific job. The procedural languages focused on organizing program statements into procedures or functions. Larger programs were either broken into functions or modules which had defined purpose and interface to other functions. Procedural approach for programming had several problems as the size of the software grew larger and larger. One of the main problems was data being completely forgotten. The emphasis was on the action and the data was only used in the entire process. Data in the program was created by variables and if more than one functions had to access data, global variables were used. The concept of global variables itself is a problem as it may be accidentally modified by an undesired function. This also leads to difficulty in debugging and modifying the program when several functions access a particular data. The object oriented approach overcomes this problem by modeling data and functions together there by allowing only certain functions to access the required data. The procedural languages had limitations of extensibility as there was limited support for creating user defined datatypes and defining how these datatypes will be handled. For example if the programmer had to define his own version of string and define how this new datatype will be manipulated, it would be difficult. The object oriented programming provides this flexibility through the concept of class. Another limitation of the procedural languages is that the program model is not closer to real world objects. For example, if you want to develop a gaming application of car race, what data
MC0066 - OOPS using C++ Roll No. XXXXXXXXX

would you use and what functions you would require is difficult questions to answer in a procedural approach. The object oriented approach solves this further by conceptualizing the problem as group of objects which have their own specific data and functionality. In the car game example, we would create several objects such as player, car and traffic signal and so on. Some of the languages that use object oriented programming approach are C++, Java, Csharp, Smalltalk etc. The key features of OOP languages are: Objects and Classes An Object is a program representation of some real-world thing (i.e person, place or an event). Objects can have both attributes (data) and behaviors (functions or methods). Attributes describe the object with respect to certain parameters and Behavior or functions describe the functionality of the object. According to Pressman, Objects can be any one of the following: a) External entities, b) Things, c) Occurrences or events, d) Roles, e) Organizational units, f) Places, g) Data Structures For example, objects can be a menu or button in a graphic user interface program or it may be an employee in an payroll application. Objects can also represent a data structure such as a stack or a linked list. It may be a server or a client in a networking environment. Objects with the same data structure and behavior are grouped together as class. In other words, Objects are instances of a class. Classes are templates that provide definition to the objects of similar type. Objects are like variables created whenever necessary in the program. Classes and Objects support data encapsulation and data hiding which are key terms describing object oriented programming languages. Data and functions are said to be encapsulated in a single entity as object. The data is said to be hidden thus not allowing accidental modification. Inheritance Inheritance is one of the most powerful features of Object Oriented Programming Languages that allows you to derive a class from an existing class and inherit all the characteristics and behavior of the parent class. This feature allows easy modification of existing code and also reuse of code. The ability to reuse components of a program is an important feature for any programming language. Polymorphism and Overloading

MC0066 - OOPS using C++

Roll No. XXXXXXXXX

Operator overloading feature allows users to define how basic operators work with objects. The operator + will be adding two numbers when used with integer variables. However when used with user defined string class, + operator may concatenate two strings. Similarly same functions with same function name can perform different actions depending upon which object calls the function. This feature of C++ where same operators or functions behave differently depending upon what they are operating on is called as polymorphism (Same thing with different forms). Operator overloading is a kind of polymorphism.

2. What is function overloading? Write a c++ program to implement a function overloaded. Ans: C++ permits the use of two functions with the same name. However such functions

essentially have different argument list. The difference can be in terms of number or type of arguments or both. This process of using two or more functions with the same name but differing in the signature is called function overloading. But overloading of functions with different return types is not allowed. In overloaded functions, the function call determines which function definition will be executed. The biggest advantage of overloading is that it helps us to perform same operations on different datatypes without having the need to use separate names for each version. The example given below is a c++ program to implement a function overloaded. The following program implements an overloaded function printline(). //fnoverload.cpp # include <iostream.h> void printline(); void printline(char ch); void printline(char ch, int n); void main() { printline(); printline(*);
MC0066 - OOPS using C++ Roll No. XXXXXXXXX

printline(*, 20); } void printline(); { for(int i=0;i<25;i++) cout<<-; cout<<endl; } void printline(char ch); {for (int i=0;i<25;i++) cout<<ch; cout<<endl; } void printline(char ch, int n); { for (int i=0;i<n;i++) cout<<ch; cout<<endl; } In the above program, the function printline() has three different prototypes depending on the arguments passed to it. The relevant function is invoked depending on the type and number of arguments passed to it.

3. Discuss the constructors and Destructors with suitable example. Ans: MC0066 - OOPS using C++ Roll No. XXXXXXXXX

Constructors and Destructors Constructors and destructors are very important components of a class. Constructors are member functions of a class which have same name as the class name. Constructors are called automatically whenever an object of the class is created. This feature makes it very useful to initialize the class data members whenever a new object is created. It also can perform any other function that needs to be performed for all the objects of the class without explicitly specifying it. Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from the constructor. They are invoked automatically whenever the objects life expires or it is destroyed. It can be used to return the memory back to the operating system if the memory was dynamically allocated. The following program implements the constructor and destructors for a class. // constdest.cpp # include<iostream.h> class sample { private: int data; public: sample() {data=0; cout<<Constructor invoked<<endl;} ~sample() {cout<<Destructor invoked;} void display() { cout<<Data=<<data<<endl;} }; void main() { sample obj1; obj.display();
MC0066 - OOPS using C++ Roll No. XXXXXXXXX

} If you run the above program you will get the output as follows: Constructor invoked Data=0 Destructor invoked When object of sample class, obj is created, automatically the constructor is invoked and data is initialized to zero. When the program ends the object is destroyed which invokes the destructor. Please note that both the constructor and destructor is declared as public and they have no return value. However, constructors can have arguments and can be overloaded so that different constructors can be called depending upon the arguments that is passed. Destructors on the other hand cannot be overloaded and cannot have any arguments. The following program implements the overloaded constructors for the distance class. //overloadconst.cpp #include<iostream.h> class distance { private: int feet; int inches; public: distance() { feet=0; inches=0;} distance(int f, int i) { feet=f; inches=i;}
MC0066 - OOPS using C++ Roll No. XXXXXXXXX

void display() { cout<<feet<<ft<<inches<<inches;} }; void main() { distance d1, d2(10,2); d1.display(); d2.display() } In the above program, the setdistance() function is replaced with the two constructors. These are automatically invoked. When object d1 is created the first constructor distance() is invoked. When object d2 is created the second constructor distance (int f, int i) is invoked as two arguments are passed. Please note that to invoke a constructor with arguments, argument values have to be passed along with the object name during declaration.

4. What do you mean by operator overloading? Illustrate with suitable example for overloading Unary operators. Ans:

Operator overloading is an interesting feature of C++ that allows programmers to specify how various arithmetic, relational and many other operators work with user defined datatypes or classes. It provides a flexible way to work with classes and can make program code look obvious. Suppose we have a class distance. To perform addition of two distance objects we use a call d3.add(d1,d2). Instead of such statements it would be more clear if we could use statements like d3=d1+d2. This is possible only if we inform compiler about how + operator works with distance class. This is exactly what operator overloading feature in C++ does. It helps to use the default operators with the user defined objects for making the code simpler. However there are several problems with operator overloading which you should be aware of. When using operator overloading, the operator should perform only the most obvious function. Otherwise it will lead to more confusion. If you are overloading + operator for distance class it should add two distance objects, and should not do something else. However some syntactical
MC0066 - OOPS using C++ Roll No. XXXXXXXXX

characteristics of operators cannot be changed even if you want to. For example, you cannot overload a binary operator to be a unary operator and vice versa. Several operators such as dot operator, scope resolution (::) operator, conditional operator (?:) etc cannot be overloaded. Therefore operator overloading should be used for a class where it is required to perform obvious functions with the default operators and there can be no other meaning for the same. To illustrator overloading unary operator let us overload the increment operator for a class. //unary.cpp # include <iostream.h> Class counter { unsigned int count; public: counter() {count=0;} int getcount() {return count;} void operator ++() { count++;} }; void main() { counter c1; c1++; ++c1; cout<<c1.getcount();

MC0066 - OOPS using C++

Roll No. XXXXXXXXX

} In the above example, the operator ++ is defined to return void and take no arguments. All unary operators do not take no arguments as it operates on only one operand and that operand itself invokes the operator. Therefore the operand is sent by default. However the above implementation cannot be used in statements such as c2=c1++; where c1 and c2 are counter variables. This is because the operator ++ returns void which makes an counter variable being assigned void value.

Questions From Book-ID : B0715 5. Write C++ program which demonstrate the difference between static and dynamic binding. Ans:

Remaining answers are available in the full assignments (in MS-WORD format).

For full assignments Contact us:

Prakash: 9686515230
Email: info@assignmentsclub.com / assignments.prakash@gmail.com Website: www.assignmentsclub.com

Note: Sample papers are available in Portable Document Format (.pdf) with a watermark of our Website. Full assignments will be in MS-WORD (.doc) format without any watermark... Contact us for Full assignments...

MC0066 - OOPS using C++

Roll No. XXXXXXXXX