Ticker

6/recent/ticker-posts

Object Oriented Programming(OOP) all Model set Solution Grade 10 Computer Engineering

 Note: Missing questions can be find at past paper solution of respective subject

Model Set-1

Group A

1.       What kind of things can become object in OOP?

àAnything that has attribute and behavior can become object in OOP.

 

2.       Define the term abstraction.

à Abstraction is the concept of object-oriented programming that “shows” only essential information and “hides” unnecessary information.

 

3.       Define cout.

à cout is the instance of the ostream class that is used to produce output on the standard output device which is usually the display screen.

 

4.       What is a default copy constructor?*

à

 

5.       When is a destructor called in OOP?

àDestructor is called whenever an object is going to be destroyed.

                                                  

6.       What does 'this' pointer point to?

à The this pointer points to the current object of the class.

 

7.       What is a virtual function?

à Virtual function is a member function which is declared within a base class and is redefined (Overriden) in a derived class.

 

8.       What is a 'postfix' notation?

à Already

 

9.       When do we use protected access specifier to a class member?

àWe use protected access specifier when we need to access members of the class from the same class and from any other class that inherits it. (i.e. it's child class)

 

10.   In what order are the class constructors called when a derived class object is created?

àWhen derived class object is created, the base class's constructors will be called first and then derived class's constructors will be called.

 

Group B

11.   How are data and function organized in oop?

à OOP languages involve using a series of classes to keep data and functions organized.  Every class can have data members and member functions and there is access specifier to determine the accessibility of these data member and member functions.

 

12.   How is the member function of a class defined? Write the syntax.

àSyntax:

                Return_type class_name :: function_name( parameter list){

                                //body of the function.

                                }

 

13.   WAP in C++ to convert a distance from kilometer to meter using inline function.

à

#include<iostream>

using namespace std;

inline int Calculate(float km){

                return (km*1000);

}

int main(){

                float disk;

                cout<<"Enter the distance in kilometer: ";

                cin>>disk;

                cout<<disk<<" kilometre = "<<Calculate(disk)<<" metre";

               

               

                return 0;

}

 

Group C

14.   Describe different types of constructor with example.

 Ã 

1.       Default Constructor:

A constructor with no parameters is known as a default constructor. In the example above, Wall() is a default constructor.

Example 1: C++ Default Constructor

#include <iostream>

using namespace std;

 

// declare a class

class  Wall {

  private:

    double length;

 

  public:

    // default constructor to initialize variable

    Wall() {

      length = 5.5;

      cout << "Creating a wall." << endl;

      cout << "Length = " << length << endl;

    }

};

 

int main() {

  Wall wall1;

  return 0;

}

Output:

Creating a Wall

Length = 5.5

Here, when the wall1 object is created, the Wall() constructor is called. This sets the length variable of the object to 5.5. Note: If we have not defined a constructor in our class, then the C++ compiler will automatically create a default constructor with an empty code and no parameters.

 

2.       C++ Parameterized Constructor:

In C++, a constructor with parameters is known as a parameterized constructor. This is the preferred method to initialize member data.

Example 2: C++ Parameterized Constructor

#include <iostream>

using namespace std;

 

// declare a class

class Wall {

  private:

    double length;

    double height;

 

  public:

    // parameterized constructor to initialize variables

    Wall(double len, double hgt) {

      length = len;

      height = hgt;

    }

 

    double calculateArea() {

      return length * height;

    }

};

 

int main() {

  // create object and initialize data members

  Wall wall1(10.5, 8.6);

  Wall wall2(8.5, 6.3);

 

  cout << "Area of Wall 1: " << wall1.calculateArea() << endl;

  cout << "Area of Wall 2: " << wall2.calculateArea();

 

  return 0;

}

Output:

Area of Wall 1: 90.3

Area of Wall 2: 53.55

Here, we have created a parameterized constructor Wall() that has 2 parameters: double len and double hgt. The values contained in these parameters are used to initialize the member variables length and height. When we create an object of the Wall class, we pass the values for the member variables as arguments.

 

3.       Copy constructor ( already done above)

 

15.   WAP in C++ to overload '+' operator to add two distanace.

à

#include<iostream>

using namespace std;

 

class Distance

{

    private:

        int km,m;

         

    public:

        //function to read distance

        void readDistance(void)

        {

            cout << "Enter Kilometre: ";

            cin >>km;

            cout << "Enter metre: ";

            cin >>m;

        }

        //function to display distance

        void dispDistance(void)

        {

            cout << "Kilometre:" << km << "\t" << "Metre:" << m<< endl;

        }

        //add two Distance using + operator overloading

        Distance operator+(Distance &dist1)

        {

            Distance tempD;     //to add two distances

            tempD.m= m + dist1.m;

            tempD.km  = km  + dist1.km + (tempD.m/1000);

            tempD.m=tempD.m%1000;

            return tempD;

        }

};

int main()

{

    Distance D1,D2,D3;

    

    cout << "Enter first  distance:" << endl;

    D1.readDistance();

    cout << endl;

    

    cout << "Enter second distance:" << endl;

    D2.readDistance();

    cout << endl;

    

    //add two distances

    D3=D1+D2;

    

    cout << "Total Distance:" << endl;

    D3.dispDistance();

 

    cout << endl;

    return 0;

}

 

16.   Explain the term 'Function overriding' with an example.

à Function overriding is a feature in OOP that allows a subclass or child class to provide a specific implementation of a function that is already provided by one of its super classes or parent classes. Function overriding cannot be done within a class. Function that is redefined must have exactly the same declaration in both base and derived class, that means same name, same return type and same parameter list. Example:

#include <iostream>

using namespace std;

class BaseClass {

public:

 virtual void disp(){

 cout<<"Function of Parent Class";

 }

};

class DerivedClass: public BaseClass{

public:

 void disp() {

 cout<<"Function of Child Class";

 }

};

int main() {

 DerivedClass obj;

 obj.disp();

 return 0;

}

Output: Function of ChildClass

 

 

Model Set-2

Group A

1.       What is the extension for C++ files?

àThe extension for C++ files is .cpp.

 

2.       How is the static member function of a class called?

àStatic member function of a class is called by using class name and :: (scope resolution operator).

 

3.       Define runtime polymorphism.

à It is the process in which a call to an overridden method is resolved at runtime rather than compile-time.

 

4.       What is the number of arguments that is required in the definition of overloaded unary operator?

àNo argument is required in the definition of overloaded unary operator.

 

5.       Define protected access specifier.

à The protected access specifier is a type of access specifier which is  used to make the member of a class accessible from that class and from any other class that inherits it.

 

Group B

6.       Consider the following segment of a program:

int a=5;

int y= a++;

cout<<"\n a="<<a;

cout<<"\ny="<<y;

                What would be the value of a and y after execution.

                Ã  The value of a would be 6 and value of y would be 5.

 

7.       WAP in C++ to initialize two data member of type int using a constructor.

à#include <iostream>

using namespace std;

 

// declare a class

class  Wall {

  private:

    int length, breadth;

 

  public:

    // default constructor to initialize variable

    Wall() {

      length = 5;

      breadth=4;

      cout << "Creating a wall." << endl;

      cout << "Length = " << length << endl;

      cout << "breadth = " << breadth;

 

    }

};

 

int main() {

  Wall wall1;

  return 0;

}

 

Group C

8.       Describe the limitation of POP.*

àCopy

 

9.       WAP in C++ to show that object are destroyed in the reverse order of creation.

à

#include <iostream>

using namespace std;

class A

{

                int id;

                static int count;

public:

                A()

                {

                                count++;

                                id = count;

                                cout << "constructor called " << id << endl;

                }

                ~A()

                {

                                cout << "destructor called " << id << endl;

                }

};

 

int A::count = 0;

int main()

{

                A a;

                A b;

                return 0;

}

 

Output:

constructor called 1

constructor called 2

destructor called 2

destructor called 1

 

From the above program, we can see that objects are always destroyed in reverse order of their creation. The reason for reverse order is, an object created later may use the previously created object. For example, consider the following code snippet.

 

A a;

B b(a);

In the above code, the object ‘b’ (which is created after ‘a’), may use some members of ‘a’ internally. So destruction of ‘a’ before ‘b’ may create problems. Therefore, object ‘b’ must be destroyed before ‘a’.

 

10.   Create a base class called shape. Use this class to store two double type values that could be used to compute the area of figures. Derive two classes called triangle and rectangle from the base shape. Add to the base class, a member function getdata() to initialize base class data members and another member function displayarea() to compute and display the area of figures. Make displayarea() as a virtual function and redefine this function in the derived classes to suit their requirements. Using these three classes, design a program that will accept dimensions of a triangle or a rectangle interactively, and display the area.**

à

 

11.   Why operator overloading is needed? Also explain about its limitations.*

à In computer programming, operator overloading, sometimes termed operator ad hoc polymorphism, is the process of making an operator to exhibit different behaviors in different instances. The primary motive of the process of operator overloading is to provide a special meaning or redefinition to an operator (unary or binary), so that it could work on user-defined data-type objects just in the same way as it works on built-in data-type type variables.

For example, we can make the operator (‘+’) for string class to concatenate two strings. We know that this is the addition operator whose task is to add two operands. So a single operator ‘+’ when placed between integer operands, adds them and when placed between string operands, concatenates them.

Following are some restrictions to be kept in mind while implementing operator overloading.

·         Precedence and Associativity of an operator cannot be changed.

·         Arity (numbers of Operands) cannot be changed. Unary operator remains unary, binary remains binary etc.

·         No new operators can be created, only existing operators can be overloaded.

·         Cannot redefine the meaning of a procedure. You cannot change how integers are added.

 

 

12.   Explain any four types of inheritance with an example.

à note

 

Model Set-3

Group A

1.       Write any two main characteristics of OOP?

àCharacteristics of OOP:

·         Emphasis is on data rather than procedures.

·         Follows bottom-up approach in program design.

 

2.       What is an object?

àObject is an instance of a class which consume some space in memory.

3.       Which header file you must include in your source program to  use cin and cout.

à<iostream> header file I must include in my source program to use cin and cout.

 

4.       What is an extension for an object file.

àThe extension for an object file is .obj  or .o .

 

5.       What is member function?

à A member function of a class is a function that has its definition or its prototype within the class definition.

 

6.       When do we make a virtual function pure?

àWe make a virtual function pure when we don’t know its implementation.

 

7.       Give one example of compile time polymorphism.

àExample of compile time polymorphism are:

·         Function overloading

·         Operator overloading

 

8.       What does term 'concatenate' mean in programming?

à Concatenation, in the context of programming, is the operation of joining two strings together.

 

9.       What is a unary operator?

à A unary operator is an operator that needs only one value/operand for its operation.

 

10.   What is a parent class?

à The class whose features are inherited is known as superclass (or a base class or a parent class).

Group B

11.   Point out the benefits of OOP.

àBenefits:

·         The principle of data hiding helps the programmer to build secure program.

·         Through inheritance, we can eliminate redundant code extend the use of existing Classes.

·         It is easy to partition the work in a project based on objects

·         Software complexity can be easily managed

 

 

12.   Illustrate the use of end manipulator.

à

                  #include<iostream>

using namespace std;

int main(){

                cout<<"Math = 90"<<endl<<"Physics=77"<<endl<<"Chemistry=56";

                return 0;

}

Output:

Math = 90

Physics=77

Chemistry=56

 

 

13.   Mention two properties of constructor.

àTwo properties of constructor are:

·         Constructor has same name as the class itself

·         Constructors don’t have return type not even void.

·         A constructor is automatically called when an object is created.

 

14.   How does a class accomplish data hiding? Explain with an example.

à Class accomplish data hiding by binding the data member and member function together and setting their accessibility using the access specifier. The ideology behind data hiding is to prevent direct access of data from outside the class.

Example: In this example, there is a class with a data member and two member functions. Here, the data member "num" is private so, it can be accessed only by the members of the same class, and it can't be accessed anywhere else. Hence, it is unable to access this data member outside the class, which is called data hiding.

#include<iostream> 

using namespace std; 

class Base{ 

      

    int num;  //by default private 

    public: 

      

    void getData(); 

    void showData(); 

      

}; 

void Base :: getData() 

    cout<< "Enter any Integer value" <<endl;  

    cin>>num; 

      

void Base :: showData() 

    cout<< "The value is " << num <<endl; 

   

int main(){ 

    Base obj;      

    obj.getData(); 

    obj.showData();  

    return 0; 

Output:

Enter any Integer value

2

The value is 2

 

               

15.   List any four arithmetic operator along with their meaning.

à

 

16.   How is polymorphism achieve at compile time?*

à Polymorphism is achieved at compile time by function overloading or operator overloading.

In both cases, the compiler has all the information about the data type and some arguments needed to select the appropriate function at compile time.

 

17.   Differentiate between private and public access specifier.

à

Public access specifier

Private access specifier

It doesn’t provide any kind of security for its data.

It provides security for its data.

Members of a class marked with public access specifier are accessible to all members of the same class and all other classes

Members of a class marked with private access specifier are accessible to only members of the same class.

If programmer doesn’t specify any access specifier in a class, it is not implemented.

If programmer doesn’t specify any access specifier in a class, it is implemented.

 

Group C

18.   WAP in C++ to add three variable and display the average using class and object.

à

#include<iostream>

using namespace std;

class Average{

                public:

                                float a,b,c,s,av;

                                void A(){

                                                cout<<"Enter three numbers: ";

                                                cin>>a>>b>>c;

                                                s=(a+b+c);

                                                av=s/3;

                                                cout<<"the average of three integers :"<<av;

                                }

};

int main(){

                Average obj;

                obj.A();

                return 0;

}

 

19.   Why C++ called OOP language? Explain.*

à C++ is called object oriented programming (OOP) language because C++ language views a problem in terms of objects involved rather than the procedure for doing it. C++ language supports the features of OOP like Classes, objects, inheritance, encapsulation, abstraction, and polymorphism.

 

20.   Differentiate between constructor and destructor.

à

 

21.   WAP in C++ that shows virtual member functions accessed with pointers.

à #include <iostream>

using namespace std;

class base

{

public:

 virtual void print ()

 { cout<< "print base class" <<endl; }

 

};

 

class derived:public base

{

public:

 void print () //print () is already virtual function in derived class, we could also declared as //virtual void print () explicitly

 { cout<< "print derived class" <<endl; }

 

 

};

//main function

int main()

{

 base *bptr;

 derived d;

 bptr = &d;

 

 //virtual function, binded at runtime (Runtime polymorphism)

 bptr->print();

 

 return 0;

}

 

Model Set-4

Group A

1.       What is an OOP?

àOOP is a programming paradigm based on the concept of class and object, and other concepts revolving around these two, such as inheritance, encapsulation, abstraction, polymorphism etc.

 

2.       Who is the developer of C++ language?

à Bjarne Stroustrup is the developer of C++ language.

 

3.       What is a fstream?
à Fstream is a header file which is used to handle the data being read from a file as input or data being written into the file as output.

 

4.       Why do we need to include header files in our program?*

à

 

5.       What is the return type of a constructor function?

àConstructor function doesn’t have return type.

 

6.       What is dynamic binding?

à Calling a function or assigning a value to a variable, at run-time is called “Dynamic Binding”.

 

7.       What is the value of 11%3?

àThe value of 11%3 is 2.

 

8.       Give one example of string constant.*

à

 

9.       Can private member of a class be accessed from derived class?

àNo, Private member of a class can't be accessed from derived class.

 

 

Group B

10.   Distinguish between the inheritance and polymorphism.

à

Inheritance

Polymorphism

Inheritance is a feature of OOP in which one class can derive properties and characteristics from another class.

Polymorphism is a feature of OOP that allows the same entity (function or operator) to behave differently in different scenarios.

Implementation of inheritance occurs in class level.

Implementation of inheritance occurs in method level.

Inheritance supports the concept of reusability and reduces code length in object-oriented programming.

Polymorphism allows the object to decide which form of the function to implement at compile-time (overloading) as well as run-time (overriding).

Inheritance can be single, hybrid, multiple, hierarchical and multilevel inheritance.

It can be compiled-time polymorphism (overloading) as well as run-time polymorphism (overriding).

 

11.   WAP in C++ to display the average of three integer number.

à

                #include<iostream>

using namespace std;

int main(){

                int a,b,c;

                float s;

                cout<<"Enter three numbers: ";

                                                cin>>a>>b>>c;

                                                s=(a+b+c)/3.0;

                                                cout<<"the average of three integers :"<<s;

                return 0;

}

 

12.   When do we declare the members of a class static?

àWe declare the members of a class static when we have to make them independent of any particular object of that class. When we declare a member of a class as static it means no matter how many objects of the class are created, there is only one copy of the static member and it is shared by all objects of that class.

 

13.   How does structure differ from class?

à The difference between class and structure:

 

14.   Mention the application of this pointer.

à There are three main applications used in this pointer. They are as follows:

·         Return Object

·         Method Chaining

·         Distinguish data members

 

15.   Why is it necessary to overload an operator?

à It is necessary to overload an operator to provide a special meaning or redefinition to an operator (unary or binary), so that it could work on user-defined data-type objects just in the same way as it works on built-in data-type type variables.

 

16.   Is it any way possible for the objects of the derived class to access the private members of a base class?*

àNo

 

Group C

17.   How data are shared by function in POP? Explain. *

à

 

18.   WAP in C++ to find area and perimeter of a rectangle. Use constructor and destructor in your program.

à

#include<iostream>

using namespace std;

class Area{

                private:

                                int len,brd, ar, pr;

                public:

                                Area(int l, int b){

                                                len= l;

                                                brd= b;

                                                ar= len*brd;

                                                cout<<"The area of rectangle is : "<<ar<<endl;

                                }

                                ~ Area(){

                                                pr= 2*(len+brd);

                                                cout<<"The perimter of rectangle is : "<<pr;

                                }

};

int main(){

                Area obj(4,5);

                return 0;

}

 

19.   WAP in C++ to overload '++' operator.

à

 

20.   WAP in C++ to illustrate the use of function overriding.

à

#include<iostream>

using namespace std;

class Program{

                public:

                                virtual void A(){

                                                cout<<"Base";

                                }

};

class Derive : public Program{

                public:

                                void A(){

                                                cout<<"derive";

                                }

};

int main(){

                Derive ob;

                                Program *b=&ob;

                b->A();

                return 0;

}

Output: derive

Reactions

Post a Comment

0 Comments