ASSIGNMENT Subject Code: BC 0037 Subject Name: Oops Using C++ Set No: 01 1. Write a program in c++ to divide a and b and display the quotient and remainder. Ans:#include #include void main() { int a,b,q,rem; clrscr(); cout<<”Enter two numbers: ”; cin>>a>>b; q= a/b; rem= a%b; cout<<”Quotient= “< #include void main() { int a[n],i,n,num,flag=0; clrscr(); cout<<”Enter the nos. of the array: “; for(i=0;i>a[i]; }
cout<<”Enter the no. to be searched: “; cin>>num; for(i=0;i
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. Destructors on the other hand are also member functions with the same name as class but are prefixed with tilde (~) sign to differentiate it from the constructor. They are invoked automatically whenever the object’s life expires or it is destroyed. It can be used to return the memory was dynamically allocated. The following program implements the Constructors and Destructors for a class: //constdest.cpp #include class sample { private: int data; public: sample() {data=0;cout<<”Constructor invoked”<
6. Overload += operator for the string class which should allow statements like should concatenate strings s1 and s2 and store the result in s1. Ans://stringer.cpp #include #include #include #include class string {char str[25]; public: string() {strcpy(str, “”);} string(char ch[]) {strcpy(str,ch);} void display() {cout<
s1+=s2. This
7. What is an inheritance? Explain different types of inheritance. Ans:- Inheritance is a very powerful feature of OOP. It allows reuse of code without modifying 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.
Shape
Circle
Rectangular
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 class inherits all the properties of child and parent classes.
Shape Rectangle
Round Rectangle
MULTILEVEL INHERITANCE The derived class can also have multiple parents which is known as multiple inheritance.
Student
Employee
Manager MULTIPLE INHERITANCE
8. Discuss polymorphism in c++. Ans:- Virtual function in C++ is 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. Shape *ptrarr[100] for(int j=0;jdraw(); This is exactly what is polymorphism.However to implement this approach several conditions should be met.
ASSIGNMENT Subject Code: BC 0037 Subject Name: Oops Using C++ Set No: 02 1. Write a program in c++ to read two floating point numbers and find their sum and average. Ans:#include #include void main() { float a,b,sum,avg; cout<<”Enter two nos.=”; cin>>a>>b; sum=a+b; avg=(a+b)/2; cout<<”The sum is = “< #include void main() { int n,I,fact=1; cout <>n; for(i=1;i<=n;i++) fact=fact*i; cout<<”Factorial of< #include int carea(int radious); void main() {
int radious; cout<<”Enter the radious”; cin>>radious; cout<<”The area of circle is”< #include class employee {private: int empid; char name[35]; int deptcode: public: void getdata() {cout<<”Enter employee code”; cin>>empid; cout<<”Enter employee name”; cin>>name; cout<<”Enter dept code; cin>>deptcode;} }; void main() {int i; employee obj[100]; cout<<”Enter employee data”; for(i=0;i<100;i++) {obj[i].getdata(); } }