(2½ Hours) [Total Marks: 75] Subject: Oops with C++ Solution Set 1. Attempt Any Three of the Following: 15 A. What Is Object

(2½ Hours) [Total Marks: 75] Subject: Oops with C++ Solution Set 1. Attempt Any Three of the Following: 15 A. What Is Object

(2½ Hours) [Total Marks: 75] Subject: OOPs with C++ Solution Set 1. Attempt any three of the following: 15 a. What is object oriented programming? State its applications. Ans: Object-oriented programming (OOP) is a software programming modelconstructed around objects. This model compartmentalizes data into objects (data fields) and describes object contents and behavior through the declaration of classes (methods). Object-oriented programming allows for simplified programming. Its benefits include reusability, refactoring, extensibility, maintenance and efficiency. Applications: • Real-time system • Simulation and modeling • Object-oriented data bases • Hypertext, Hypermedia, and expertext • AI and expert systems • Neural networks and parallel programming • Decision support and office automation systems • CIM/CAM/CAD systems b. Illustrate the relationship between object and class. Ans: 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 class. Each object is associated with the data of type class with which they are created. A class is thus a collection of objects similar types. For examples, Mango, Apple and orange members of class fruit. c. Explain the concept of abstraction with suitable example. Ans: Abstraction refers to the act of representing essential features without including the background details or explanation. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, wait, and cost, and function operate on these attributes. They encapsulate all the essential properties of the object that are to be created. The attributes are sometime called data members because they hold information. The functions that operate on these data are sometimes called methods or member function. d. Explain in brief about reusability with suitable example. Ans: Inheritance ( Reusability) is the process by which objects of one class acquired the properties of objects of another classes. It supports the concept of hierarchical classification. For example, the bird, ‘robin’ is a part of class ‘flying bird’ which is again a part of the class ‘bird’. e. What is polymorphism? Give suitable example for the same. Ans: Polymorphism means the ability to take more than on form. An operation may exhibit different behavior is different instances. The behavior 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 exhibit different behaviors in different instances is known as operator overloading . f. Write a note on dynamic binding. Ans: Binding refers to the linking of a procedure call to the code to be executed in response to the call. Dynamic binding means that the code associated with a given procedure call is not known until the time of the call at run time. It is associated with polymorphism and inheritance. A function call associated with a polymorphic reference depends on the dynamic type of that reference. 2. Attempt any three of the following: 15 a. Explain the structure of C++ class. Ans: Class MyClass { private: datatype var; public: void Method1() { Define Method // } }; void main() { MyClassobj; Obj.Method1(); } b. Write a C++ program to create a class Bank with { acno, custname, bal} as its attributes. And implement the methods withdraw() , deposit() and showBalance(). Class Bank { private: intaccno; char custname[40];double bal; public: void withdraw() { Define Method // } void deposit() { Define Method // } void showBalance() { Define Method // } }; void main() { Bank obj; obj.withdraw(); obj.deposit(); obj.showBalance(); } c. Explain in brief the concept of friend function and class with suitable example. lass Box { double width; public: friend void printWidth( Box box ); void setWidth( double wid ); }; // Member function definition void Box::setWidth( double wid ) { width = wid; } // Note: printWidth() is not a member function of any class. void printWidth( Box box ) { /* Because printWidth() is a friend of Box, it can directly access any member of this class */ cout<< "Width of box : " <<box.width<<endl; } // Main function for the program int main() { Box box; // set box width without member function box.setWidth(10.0); // Use friend function to print the wdith. printWidth( box ); return 0; } d. What is constructor? State its characteristics. A constructor (having the same name as that of the class) is a member function which is automatically used to initialize the objects of the class type with legal initial values. These have some special characteristics. These are given below: (i) These are called automatically when the objects are created. (ii) All objects of the class having a constructor are initialized before some use. (iii) These should be declared in the public section for availability to all the functions. (iv) Return type (not even void ) cannot be specified for constructors. (v) These cannot be inherited, but a derived class can call the base class constructor. (vi) These cannot be static. (vii) Default and copy constructors are generated by the compiler wherever required. Generated constructors are public. (viii) These can have default arguments as other C++ functions. (ix) A constructor can call member functions of its class. (x) An object of a class with a constructor cannot be used as a member of a union. (xi) A constructor can call member functions of its class. (xii) We can use a constructor to create new objects of its class type by using the syntax e. Write a C++ program to implement the concept of constructor and destructor. class add { private : int num1,num2,num3; public : add(int=0, int=0); //default argument constructor //to reduce the number of constructors void sum(); void display(); ~ add(void); //Destructor }; Add:: ~add(void) { Num1=num2=num3=0; Cout<<”\nAfter the final execution, me, the object has entered in the” <<”\ndestructor to destroy myself\n”; } //Constructor definition add() Add::add(int n1,int n2) { num1=n1; num2=n2; num3=0; } //function definition sum () Void add::sum() { num3=num1+num2; } //function definition display () Void add::display () { cout<<”\nThe sum of two numbers is “<<num3<<end1; } void main() { Add obj1,obj2(5),obj3(10,20): Obj1.sum(); //function call Obj2.sum(); Obj3.sum(); cout<<”\nUsing obj1 \n”; obj1.display(); //function call cout<<”\nUsing obj2 \n”; obj2.display(); cout<<”\nUsing obj3 \n”; obj3.display(); } f. Explain the concept of pointer to object with suitable example. Ans: A variable that holds an address value is called a pointer variable or simply pointer. Pointer can point to objects as well as to simple data types and arrays. sometimes we dont know, at the time that we write the program , how many objects we want to create. when this is the case we can use new to creat objects while the program is running. new returns a pointer to an unnamed objects. class student { private: introllno; string name; public: student():rollno(0),name("") {} student(int r, string n): rollno(r),name (n) {} void get() { cout<<"enter roll no"; cin>>rollno; cout<<"enter name"; cin>>name; } void print() { cout<<"roll no is "<<rollno; cout<<"name is "<<name; } }; void main () { student *ps=new student; (*ps).get(); (*ps).print(); delete ps; } 3. Attempt any three of the following: 15 a. Explain the concept of function overloading with suitable example. long add(long, long); float add(float, float); int main() { long a, b, x; float c, d, y; cout<< "Enter two integers \n"; cin>> a >> b; x = add(a, b); cout<< "Sum of integers: " << x <<endl; cout<< "Enter two floating point numbers \n"; cin>> c >> d; y = add(c, d); cout<< "Sum of floats: " << y <<endl; return 0; } long add(long x, long y) { long sum; sum = x + y; return sum; } float add(float x, float y) { float sum; sum = x + y; return sum; } b. Write a C++ program to overload binary (++) operator. Ans: #include<iostream> usingnamespacestd; classBox{ double length;// Length of a box double breadth;// Breadth of a box double height;// Height of a box public: doublegetVolume(void){ return length * breadth * height; } voidsetLength(doublelen){ length =len; } voidsetBreadth(doublebre){ breadth =bre; } voidsetHeight(doublehei){ height =hei; } // Overload + operator to add two Box objects. Box operator +( const Box & b){ Boxbox; box.length=this->length +b.length; box.breadth=this->breadth +b.breadth; box.height=this->height +b.height; return box; } }; // Main function for the program int main(){ BoxBox1;// Declare Box1 of type Box BoxBox2;// Declare Box2 of type Box BoxBox3;// Declare Box3 of type Box double volume =0.0;// Store the volume of a box here // box 1 specification Box1.setLength(6.0); Box1.setBreadth(7.0); Box1.setHeight(5.0); // box 2 specification Box2.setLength(12.0); Box2.setBreadth(13.0); Box2.setHeight(10.0); // volume of box 1 volume =Box1.getVolume(); cout<<"Volume of Box1 : "<< volume <<endl; // volume of box 2 volume =Box2.getVolume(); cout<<"Volume of Box2 : "<< volume <<endl; // Add two object as follows: Box3=Box1+Box2; // volume of box 3 volume =Box3.getVolume(); cout<<"Volume of Box3 : "<< volume <<endl; return0; } c. List the operators that cannot be overloaded. Explain the rules for overloading the operators. Ans: Operators that cannot be overloaded::: , .*, . , ?: Rules: 1. Only existing operators can be overloaded. New operators cannot be overloaded. 2. The overloaded operator must have at least one operand that is of user defined type. 3. We cannot change the basic meaning of an operator. That is to say, We cannot redefine the plus(+) operator to subtract one value from the other. 4. Overloaded operators follow the syntax rules of the original operators. They cannot be overridden. 5. There are some operators that cannot be overloaded like size of operator(sizeof), membership operator(.), pointer to member operator(.*), scope resolution operator(::), conditional operators(?:) etc 6. We cannot use “friend” functions to overload certain operators.However, member function can be used to overload them.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    17 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us