Oosd Lab File
Oosd Lab File
Practical File
OOSD
(5th semester)
B.tech(CSE)
OUTPUT
0
45
2.WAP IN C++ FOR INLINE FUNCTION
#include<iostream>
using namespace std;
inline int cube(int n){
return n*n*n;
}
int main(){
int n,result=0;
cout<<"enter the value of n:"<<endl;
cin>>n;
result=cube(n);
cout<<"cube of n is:"<<result<<endl;
return 0;
}
OUTPUT
enter the value of n:
5
cube of n is:125
3.WAP IN C++ BY CREATING A CLASS INTEGER AND WRITE A
FUNCTION THAT PRINTS ALL THE PRIME NUMBERS FROM THE
CLASS
#include <iostream>
using namespace std;
class IsPrime {
private:
int start, end;
public:
void getRange() {
cout << "Enter Range:";
cin >> start >> end;
}
void isPrime() {
int index_1, index_2;
cout << "Prime Numbers in the range " << start << " to " << end << " are " << endl;
for (index_1 = start; index_1 <= end; index_1++) {
int check = 0;
for (index_2 = 2; index_2 < index_1; index_2++) {
if (index_1 % index_2 == 0){
check++;
break;
}
}
if (check == 0) {
cout << index_1 << " ";
}
}
}
};
int main() {
IsPrime P;
P.getRange();
P.isPrime();
return 0;
}
OUTPUT
Enter Range:3 30
Prime Numbers in the range 3 to 30 are
3 5 7 11 13 17 19 23 29
4.WAP IN C++ FOR MULTIPLE INHERITANCE
#include<iostream>
using namespace std;
class A{
public:
int a=10;
};
class B :public A{
public:
int b=20;
};
class C: public B{
public:
void show(){
cout<<"value of a is "<<a<<endl;
cout<<"value of b is "<<b<<endl;
}
};
int main(){
C obj;
obj.show();
}
OUTPUT
value of a is 10
value of b is 20
5. WAP IN C++ TO OVERLOAD BINARY OPERATOR
#include<iostream>
using namespace std;
class overloading {
int value;
public:
void setValue(int temp) {
value = temp;
}
overloading operator+(overloading ob) {
overloading t;
t.value = value + ob.value;
return (t);
}
void display() {
cout << value << endl;
}
};
int main() {
overloading obj1, obj2, result;
int a, b;
cout << "Enter the value of Complex Numbers a,b:";
cin >> a>>b;
obj1.setValue(a);
obj2.setValue(b);
result = obj1 + obj2;
cout << "Input Values:\n";
obj1.display();
obj2.display();
cout << "Result:";
result.display();
return 0;
}
OUTPUT
Enter the value of Complex Numbers a,b:1 6
Input Values:
1
6
Result:7
6.WAP IN C++ TO IMPLEMENT FRIEND FUNCTION
#include <iostream>
using namespace std;
class base {
private:
int private_variable;
protected:
int protected_variable;
public:
base()
{
private_variable = 10;
protected_variable = 20;
}
friend void friendFunction(base& obj);
};
void friendFunction(base& obj)
{
cout << "Private Variable: " << obj.private_variable
<< endl;
cout << "Protected Variable: " << obj.protected_variable;
}
int main()
{
base object1;
friendFunction(object1);
return 0;
}
OUTPUT
Private Variable: 10
Protected Variable: 20
7. WAP IN C++ TO OVERLOAD ‘+’ OPERATOR TO CONCATENATE
TWO STRINGS
#include <iostream>
#include <string.h>
using namespace std;
class AddString {
public:
char s1[25], s2[25];
AddString(char str1[], char str2[])
{
strcpy(this->s1, str1);
strcpy(this->s2, str2);
}
void operator+()
{
cout << "\nConcatenation: " << strcat(s1, s2);
}
};
int main()
{
char str1[] = "Hello";
char str2[] = "World";
AddString a1(str1, str2);
+a1;
return 0;
}
OUTPUT
Concatenation: HelloWorld
8. WAP TO DEMONSTRATE HOW AMBIGUITY IS AVOIDED IN A
SINGLE INHERITANCE USING SCOPE RESOLUTION OPERATOR
#include <iostream>
using namespace std;
class Shape {
public:
void draw() {
cout << "Drawing a generic shape." << endl;
}
};
class Circle : public Shape {
public:
void draw() {
cout << "Drawing a circle." <<endl;
}
};
class Rectangle : public Shape {
public:
void draw() {
cout << "Drawing a rectangle." <<endl;
}
};
int main() {
Rectangle rect;
rect.draw();
rect.Shape::draw();
return 0;
}
OUTPUT
Drawing a rectangle.
Drawing a generic shape.
int main()
{
student s;
s.display();
return 0;
}
OUTPUT
Enter the RollNo:38
Enter the Name:MAHAK
Enter the Fee:50000
38 MAHAK 50000
10.WAP IN C++ TO IMPLEMENT POLYMORPHISM
#include <iostream>
using namespace std;
class Animal {
public:
void eat(){
cout<<"Eating";
}
};
class Dog: public Animal
{
public:
void eat()
{ cout<<"Eating bread";
}
};
int main(void) {
Dog d = Dog();
d.eat();
return 0;
}
OUTPUT
Eating bread
11.WAP IN C++ FOR VIRTUAL FUNCTION
#include <iostream>
using namespace std;
class base {
public:
virtual void print() {
cout << "print base class\n";
}
void show() {
cout << "show base class\n";
}
};
class derived : public base {
public:
void print() {
cout << "print derived class\n";
}
void show() {
cout << "show derived class\n";
}
};
int main()
{
base* bptr;
derived d;
bptr = &d;
bptr->print();
bptr->show();
return 0;
}
OUTPUT
print derived class
show base class