Está en la página 1de 8

Name: Rajat Chowdhry Roll Number: 520810922 Learning Centre: 2017 Subject Code: BC0037 Subject: OOPS Using

C++ Assignment No.: 1 Course: Bachelor Of Computer Application (II Semester) Date of Submission at the Learning Centre: 8th June, 2009

Ques. 1.

Write a program in c++ to divide a and b and display the quotient and remainder

Ans.

# include<iostream.h> void main() { int a,b,q,rem; cout<<Enter two numbers; cin>>a>>b; q=a/b; r=a%b; cout<<Quotient=<<q<<endl; cout<<Remainder=<<r; }

Ques. 2. Ans.

Explain Break and continue statements in c++.

BREAK statement transfers the control to the next statement after the loop and all thee

remaining statements and the iterations in the loop are skipped. It is used if you would like to exit the loop when a certain condition is met. In the switch statement break statement takes the control to the next statement after the switch statement.
CONTINUE statement is used to take the control to the next iteration of the loop. It is used

if the control has to be transferred to the iteration of the loop based on a condition skipping all the other statements in the loop.

Ques. 3.

Write a program in c++ to read N number in array, the user should be able to search a particular number in the aaray using sequential search algorithm.

Ans.

#include<iostream.h> #include<conio.h> void main() { int a,i,e[100],n,flag=0; cout<<"Enter Size of array"; cin>>n; for(a=0;a<n;a++) { cin>>e[a]; } cout<<"Enter Number to be searched"; cin>>i;

for(a=0;a<n;a++) { if(e[a]==i) { flag=1; break; } } if(flag==1) cout<<"Number found at "<<a; else cout<<"Not Found"; getch(); }

Ques. 4. Ans.

Explain the scope and visibility of variables in c++ functions Scope and Visibility

C++ has some rules for the naming of variables and their range of accessibility. The scope of a variable is limited to the region within the nearest enclosing braces. Outside that region, the variable does not exist. The exceptions are 1. If a same-name variable is declared within a subsidiary structure (like our for-loop above), this "inside" variable will temporarily hide the original variable, 2. A global variable is declared, one that has no enclosing braces.

Ques. 5.

Explain concepts of constructors and destructors with an example

Ans.

CONSTRUCTORS allow initialization of objects at the time of their creation. Constructor

function is a special function that is a member of the class and has same name as that of the class. An objects constructor is automatically called whenever the object is created (statically or dynamically). Constructors are always public. They are used to make initializations at the time of object creation. Consider following example of a stack class:

#include <iostream> using namespace std; class stack { int top, bottom; int data[20]; stack() { top = bottom; cout <<Inside Stack Constructor: Stack initialized; } int stackfull() { return ((top == max 1)?1:0); } int stackempty() { return (top == bottom)?1:0); } void push(int no) { if(stackfull()) cout<<Stack is full; else data[++top] = no; } int pop() { if(stackempty()) cout<<Nothing to pop.. Stack is Empty!; else return(data[top--]; } }; int main() { int i, no; stack st; cout << Entered Main\n; for(i = 0; i < 10; i++) st.push(i); no = s.pop(); cout <<The popped element is: return 0; }

DESTRUCTORS are complements of constructors. When an object is destroyed, its

destructor is automatically called. Destructors are mainly useful for doing the clean up job. E.g. an object may have allocated some memory during its lifetime; destructors are the place where this memory is deallocated. Or an object may need to close some files by releasing its handles which it had previously obtained. Destructor function has same name as that of a constructor; but the name is preceded by a tilde (~) sign. We will expand the above example of a stack class to include a destructor: #include <iostream> using namespace std; class stack { int top, bottom; int data[20]; stack() { top = bottom; cout <<Inside Stack Constructor: Stack initialized\n; } int stackfull() { return ((top == max 1)?1:0); } int stackempty() { return (top == bottom)?1:0); } void push(int no) { if(stackfull()) cout<<Stack is full; else data[++top] = no; } int pop() { if(stackempty()) cout<<Nothing to pop.. Stack is Empty!\n; else return(data[top- -]; } ~stack() { cout <<Inside Stack Destructor: Stack Destroyed\n; }

}; int main() { int i, no; stack st; cout <<Entered Main\n; for(i = 0; i < 10; i++) st.push(i); no = s.pop(); cout <<The popped element is:<<no << \n; return 0; }

Ques. 6.

Overload += operator for the string class which should allow statements like s1+=s2. This should concatenate strings s1 and s2 and store the result in s1.

Ans.

#include<iostream.h> #include<ctype.h> #include<string.h> #include<conio.h> class string { char str[25]; public: string() { strcpy(str,); } string(char ch[]) { strcpy(str, ch); } void display() { cout<<str; } string operator ++() { string temp; int i; for(i=0; str[i]!=\0; i++) temp.str[i]=toupper(str[i]); temp.str[i]=\0;

return temp; } }; void main() { clrscr(); string s1=hello, s2; s2=s1++; s2.display(); getch(); }

Ques. 7. Ans.

What is an inheritance? Explain different types of inheritance?

Inheritance is a very powerful feature of object-oriented programming. It allows reuse of code without modifying the original code. It also allows flexibility to programmer to make modifications to the program without altering the original code which saves debugging and programming time and effort. TYPES OF INHERITANCE Shape Circle
Rectangle

Triangle

Hierarchial Inheritance example

Inheritance can also be multilevel inheritance where another class is derived from the derived class. In such case the grand child inherits all the properties of child and parent classes. Shape

Rectangle Rounded Rectangle


Multilevel Inheritance

The derived class can also have multiple parents, which is known as multiple inheritances. Here the child or derived class has two or more parent classes. The child class inherits all

the properties of all its parents. Multiple inheritance is implemented same as single inheritance except that both the parent names have to be specified while defining the class. Student Employee

Manager
Multiple Inheritance

In the above example we have created a class employee and a student. Manager class is defined from both of these classes. This is useful in instance when you want to create an employee whose educational qualifications need not be stored such as worker.
Ques. 8. Ans. Discuss polymorphism in c++.

Virtual functions in C++ are important to implement the concept of polymorphism. Polymorphism means same content but different forms. In C++, polymorphism enables the same program code calling different functions of different classes. Imagine a situation where you would like to create a class shape and derive classes such as rectangle, circle, triangle, etc. Let us suppose each of the classes has a member function draw() that causes the object to be drawn on the screen. This is a very desirable capability that completely different functions are executed by same function call. This is exactly what is polymorphism. However to implement this approach several conditions should be met. Firstly, all the classes rectangle, circle, triangle should be derived from a single class (Here it is shape class). Secondly, the draw() function must be declared as virtual in the base class (in the shape class).

También podría gustarte