Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

Rohit.

Kumar Roll No: 520776763 BCA 2nd Semester BC0037 01 Object Oriented Programming Using C++ Question 1 - Distinguish between procedural language and OOP language. And explain the key feature of OOP. Ans. Difference between Procedural Language and OOP Language: 1. Procedural language focuses on the organizing program statement into procedures or function. Larger programs were either broken into functions or modules whereas in Object Oriented Programming bundles both data and function into one unit known as object. 2. One of the problems with Procedural approach for programming war data being completely forgotten. The emphasis was on the action and the data was only used in the entire process. Whereas in object oriented approach overcomes this problem by modeling data and function together there by allowing only certain functions to access the required data. 3. The procedural language had limitations of extensibility as there was limited support for creating user define data type and defining how these data type will be handled whereas in OOP language provides this flexibility thought the concept of class. 4. Another limitation of the procedural languages is that the program model is not closer to real world objects In the object Oriented approach solve this further by conceptualizing the problem as group of object which have their own specific data and functionality in the car game example, we would create several object such as player, car, traffic, signal and so on. Key Feature of OOP: Encapsulation: Data and function are said to be encapsulated in a single entity as object Data Hiding: The data is said to be hidden thus not allowing accidental modification. Inheritance: it is one of the most powerful features of Object Oriented Programming language that allows you to derive a class from an existing class and inherit all the characteristic and behavior of the parent class. This feature allows easy modification of existing code and also reuses code. The ability to refuse components of a program is an important feature for any programming language
1

Polymorphism: it is ability of object belonging to different type to respond to method calls of method of the same name, each one according to an appropriate type-specific behavior. The programmer does not have to know the exact type of the object in advance, so this behavior can be implemented at run time. Overloading: operator overloading feature allows user to define how basic operators work with objects. This operator + will be adding two number when used with integer variable. However when used with used define string class, + operator may concatenate two string. Similarly same function with same function name can perform different action depending upon which object calls the function. This feature of C++ where same operator or function behave differently depending upon what they are operating an is called as polymorphism. Operator overloading is a kind of polymorphism.

Question 2 - With a suitable example show that switch () statement differ from the normal if ().else statement. Ans. #include<conio.h> #include<iostream.h> Void main () { Int d; Clrscr(); Cout<<Please enter numeric valu\n; Cin>>d; Switch(d) { Case 10; Cout<<switch cas A exected\n; Break; Case 20 ; Cout<<switch case B executed\n; Break;
2

} If(d==30) { Cout<<if case C executed\n; } else { Cout<<Else Executed\n; { Cout<<Else Executed\n; } Getch(); } Question 3 - What is function overloading? Write a C++ program to implement a function overloading.

Ans. More than one user defined function can have same name and perform different operations. This is a powerful feature of C++ and is known as Function overloading . Every overloaded function however have a different prototype. The following program implements a Overloaded Function #include<iostream.h> Void printline(); Void printline(char ch); Void printline(char ch, int n); Void main(){ Printline(); Printline(*); Printline(*20);} Void printline(); { for(int i=0;i<25;i++) Cout<<*; Cout<<endl; } Void printline(char ch);
3

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

Question 4 - Explain the scope and visibility of variables in C++ function. Ans. Function save memory as all the calls to the function causes the same code to be executed. However, with this there is overhead of execution time as there be instructions for jump to the function, saving data in registers, pushing arguments to stack and remove them, restoring data, instruction for returning to the calling program and return them, restoring data, instructions for returning to the calling program and return value. To save execution time of function, one of the features in C++ is inline function. An inline function declaration begins with the keyword inline. This type of function is suitable for small function which has to be repeatedly called. This enables the user to write programs using function which would make programs look neat and less lengthy, at the same time the actual code is inserted in all the places where the function is called after compilation enabling faster execution of the program. The several type of variables can be created in C++ Which can be different in terms of life, visibility and the way it is initialized. There are also known as storage classes.

Question 5 - Discuss the constructors and Destructors with suitable example. Ans. Construction and deconstruction are member function 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.

The following program implements the constructor and destructors for a class #include<iostrem.h> Class sample { private; int data; public;
4

sample(){data=0;cout<<Constructor invokes<<endl;} ~sample(){cout<<destructor invoked;} Void main() { sample obj1; Obj.display();} If u run the above program you get the output as follows; Constructor invoked Data=0 Destructor invoked

Question 6 - Write a program in C++ to compute the LCM of two numbers. Ans. Assumed that the gcd and lcm of negative value are the same as their positive equivalent; e.g. Lcm (-8,12)=lcm(8,12) = 24 Int gcd (int a,int b); { if (a<0) a=-a; if(b<0) b=-b; if(b==0) return a; else return gcd (b,a%b); } // now , using GCD, you may compute LCM as a*b / GCD (a,b): Int lcm (inta , intb) { if (a==0 && b==0) return 0; if(a<0) a=-a; if(b<0) b=-b; return a*b / gcd (a,b); }

Question 7 - What do you mean by operator overloading? Illustrate with suitable example for overloading unary operators. Ans. Operator Overloading It is an interesting feature of C++ that allows programmers to specify how various arithmetic, relational and many other operators work with user defined data types or classes.
5

Overloading unary operators It works similar to any member function of a class. But it is not invoked using dot operator. Just like member function the operator has a return value and takes argument. It is invoked by the operand which uses it. In case of overloading of unary operators, the calling operand can be either lift or right of the operator like in case of increment and decrement operator. While defining the operator functionality for the class the keyword operator is used . let us overload the increment operator #include<iostrem.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(); } Question 8 - Illustrate with coding the implementation of inheritance with your own example . Ans. #include <iostream.h> #include<conio.h> class BaseClass { int i; public: void setInt(int n); int getInt(); };

class DerivedClass : public BaseClass { int j; public: void setJ(int n); int mul(); }; void BaseClass::setInt(int n) { i = n; } int BaseClass::getInt() { return i; } void DerivedClass::setJ(int n) { j = n; } int DerivedClass::mul() { return j * getInt(); } int main() { clrscr(); DerivedClass ob; ob.setInt(10); // load i in BaseClass
7

ob.setJ(4);

// load j in DerivedClass // displays 40

cout << ob.mul(); getch(); return 0; }

You might also like