You are on page 1of 9

Question 1: Distinguish between procedural language and OOP language. And Explain the key features of OOP.

Distinguish between Procedural Language and OOPs


Object oriented programming is a programming paradigm where both the data and the functions are built into one unit which is known as object. And the function of particular object can only access in the object providing high level of security for the data. The functions in the object are known as methods or member functions. Whereas in Procedural languages are just that the program moves from procedure to procedure in a pre-set order, and there is very little interaction with the user.

Key Features of OOPs


Data Abstraction and Encapsulation: Abstraction refers to the act of represent essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes. Encapsulation refers to the act of Storing data and functions in a single unit which are called classes. And data can be accessible only to function witch are stored in the class and cannot be accessible to outside world. Inheritance: This feature of Object oriented programming allows one data type to acquire properties of other data types. This can be declared as public, protected, or private. And they are called access specifier and they determines whether unrelated and derived classes can access the inherited public and protected members of the base class. Polymorphism: Polymorphism gives ability to create a variable, a function, or an object that has more than one form. An operation may exhibit different behaviours in different instances. The behaviour depends on the data types used in the operation.

Question 2: With a suitable example show that switch Statement differs from the normal if.else Statement.

If-Else Statement
The if-else statement is used to express decisions. Formally the syntax is: if (expression) Statement 1 else Statement 2 In the above syntax Statement 1 will execute if expression is true otherwise Statement 2 will execute. Since an if tests the numeric value of an expression, certain coding shortcuts are possible.

Example:

#include<iostream> int main() { int integer; //Promt user for integer and obtain integer cout << "Please enter an integer"<< endl; cin >> integer; //Determine if integer is even or odd if (integer % 2 == 0) cout << integer << "is even" << endl; else cout << integer << "is odd" << endl; return 0; }

Switch Statement
The switch statement is a multi-way decision that tests whether an expression matches one of a number of constant integer values, and branches accordingly. switch (expression) { caseconst-expr : statements caseconst-expr : statements default: statements } Each case is labelled by one or more integer-valued constants or constant expressions. If a case matches the expression value, execution starts at that case. All case expressions must be different. The case labelled default is executed if none of the other cases are satisfied. A default is optional; if it isn't there and if none of the cases match, no action at all takes place. Cases and the default clause can occur in any order.
2

Example:
#include<stdio.h> main() /*count digits, white, others */ { int c, i, nwhite, nother, ndigit[10]; nwhite = nother = 0; for(i=0; i<10; i+++) ndigit[i]=0; while((c-getchar()) != EOF) { switch(c) { case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': ndigit[c-'0']++; break; case '': case '\n': case '\t': nwhite++; break; default: nother++; break; } } printf("Digits = "); for(i=0; i<10; i+++) printf("%d", ndigit[i]); printf(", White Space = %d, Other = %d\n", nwhite, nother); return 0; }

Question 3: What is function overloading? Write a C++ program to implement a Function overloaded.

Function Overloading

Function Overloading is a feature that enable to use the same name for two or more functions but their parameter declarations are different. And in this situation, the functions that share the same name are said to be overloaded, and the process is referred to as function overloading

Example:
#include<iostream.h>

#include<conio.h> #include<math.h> int pow(int,int); double pow(double,int); main() { float m, n, y=0; clrscr(); cout<<"\n Enter the value of m: "; cin>>m; cout<<"\n Enter the value of n: "; cin>>n; pow(m,n); y=pow(m,n); cout<<m<<" power " <<n<< " is : " <<y; getch(); return 0; } double pow(double m,int n) { double p; p=pow(m,n); return p; } int pow(int m,int n) { double q; q=pow(m,n); return(0); }

Question 4: Explain the scope and visibility of variables in C++ functions. Answer: The scope and visibility of variable depends on the scoping identifier, like, auto, external, static and register. Whenever we define a variable, without any of these, they are by default auto. In case of automatic variables, the life of the variable starts when the function is invoked and ends when the function terminated. Its only this particular function for whom the variable is visible. Static variables, when defined inside the function, the life starts when the function is invoked first time, and stays until the program ends, this means the variable's life is like a global variable, but it can only be used within the function. Static variable, when defined outside the function, they are very much similar to global variable where any function can access it but only within the file where it has been defined.
4

External variable are global variables, which can be access anywhere in the program, even from with other file. Register variable are local variables, defined inside a function, which is given a register space instead of RAM, but this is dependent on register space availability and it is decided by the OS. Question 5: Discuss the Constructors and Destructors with suitable example.

Constructors
A constructor is a special method that is created when the object is created or defined. This particular method holds the same name as that of the object and it initializes the instance of the object whenever that object is created. The constructor also usually holds the initializations of the different declared member variables of its object. Unlike some of the other methods, the constructor does not return a value, not even void.

Destructors
Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted. The Destructors also does not return a value, not even void. A destructor is a member function with the same name as its class prefixed by a ~ (tilde)

Example:
Implements the constructor and destructors for a class # 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}
5

} void main() { sample obj1 obj.display() } Question 6: Write a program in C++ to compute the LCM of two numbers.

/* Write a C++ program to Compute the LCM of two Numbers */


#include<iostream.h> int HCF(int x, int y) { int tmp; if(x<y) { tmp=x; x=y; y=tmp; //using tmp to swap x and y } while(x%y!=0) { tmp=x%y; //using tmp to store remainder y=x; x=tmp; } return(y); } int LCM(int x, int y) { return(x*y/HCF(x,y)); } int main() { int k,a,b; cout<<"Enter First value: "; cin>>a; cout<<"Enter Second value: "; cin>>b; k=LCM(a,b); cout<<"\nLCM of "<<a<<" and "<<b<<" is:"<< k; }

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

Operator Overloading

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 data types or classes. It provides a
6

flexible way to work with classes and can make program code look obvious. Unary operators are those operators which work on one operator. Operator overloading 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 arguments. It is invoked by the operand which uses it. In case of overloading of unary operators, the calling operand can be either left or right of the operator like in case of increment and decrement operators. While defining the operator functionality for the class the keyword operator is used.

/* Program to Illustrate the Operator Overloading for Unary Operators */


# 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() }

Question 8: Illustrate with coding the implementation of inheritance with your own example.

Inheritance

Inheritance is a very powerful feature of object oriented programming. Inheritance allows reuse of code without modifying the original code and allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort. Inheritance feature has enabled to distribute class libraries which allows the programmer to use the standard code developed by some another company. Inheritance is the process of creating new classes known as derived classes from the existing or base class. The features of the base class are said to be inherited by the
7

derived class. The child class has all the functionality of the parent class and has additional functionalities of its own.

/* Implementation of Inheritance Program */


# include<iostream.h> # include<string.h> # include<conio.h> class employee { protected: int empno char ename[25] public: employee() Class Base Private Protected Public Not Allowed Private Protected Public Class Derived: public Base Base object Derived object { empno=0 strcpy(ename,"") } employee(int n, char ch[25]) { empno=n strcpy(ename,ch) } void display() { cout<<"Emp Code:"<<empno cout<<"Name:"<<ename } } class manager: public employee { protected: float basic float hra public: manager():employee() { basic=0.0 hra=0.0 } manager(int n,char ch[25],float i, float j): employee(n,ch) { basic=i hra=j } void display()

{ employee::display() cout<<"Basic"<<basic<<endl cout<<"HRA"<<hra<<endl } } void main() { clrscr() employee e1(106,"amit") manager m1(205,"pawan",40000.00,5000.00) e1.display() m1.display() getch() }

You might also like