0% found this document useful (0 votes)
20 views

Oosd Lab File

The document appears to be a practical file submission for an Object Oriented Software Development (OOSD) course. It includes 11 programming assignments in C++ covering topics like constructor overloading, inheritance, operator overloading, polymorphism and virtual functions. For each assignment, it provides the code snippet, output and a brief description. The file is submitted to the faculty member for evaluation and signature.

Uploaded by

mahakmahak49793
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Oosd Lab File

The document appears to be a practical file submission for an Object Oriented Software Development (OOSD) course. It includes 11 programming assignments in C++ covering topics like constructor overloading, inheritance, operator overloading, polymorphism and virtual functions. For each assignment, it provides the code snippet, output and a brief description. The file is submitted to the faculty member for evaluation and signature.

Uploaded by

mahakmahak49793
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Session:2023-2024

Practical File
OOSD
(5th semester)
B.tech(CSE)

Submitted To: Submitted By:


Mr.Shobhit Sharma Mahak
2102400100051
INDEX

S.no. TOPIC PAGE TEACHER’S


NO. SIGNATURE
1. WAP IN C++ FOR CONSTRUCTOR OVERLOADING 1

2. WAP IN C++ FOR INLINE FUNCTION 2

3. WAP IN C++ BY CREATING A CLASS INTEGER AND 3-4


WRITE A FUNCTION THAT PRINTS ALL THE PRIME
NUMBERS FROM THE CLASS

4. WAP IN C++ FOR MULTIPLE INHERITANCE 5

5. WAP IN C++ TO OVERLOAD BINARY OPERATOR 6-7

6. WAP IN C++ TO IMPLEMENT FRIEND FUNCTION 8-9

7. WAP IN C++ TO OVERLOAD ‘+’ OPERATOR TO 10


CONCATENATE TWO STRINGS

8. WAP TO DEMONSTRATE HOW AMBIGUITY IS 11


AVOIDED IN A SINGLE INHERITANCE USING
SCOPE RESOLUTION OPERATOR

9. WAP IN C++ TO DEMONSTRTAE CONSTRUCTOR 12-13

10. WAP IN C++ TO IMPLEMENT POLYMORPHISM 14

11. WAP IN C++ FOR VIRTUAL FUNCTION 15-16


1.WAP IN C++ FOR CONSTRUCTOR OVERLOADING
#include<iostream>
using namespace std;
class construct{
public:
float area;
construct(){
area=0;
}
construct(int a,int b){
area=a*b;
}
void display(){
cout<<area<<endl;
}
};
int main(){
construct obj;
construct obj1(5,9);
obj.display();
obj1.display();
return 0;
}

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.

9. WAP IN C++ TO DEMONSTRTAE CONSTRUCTOR


#include<iostream>
using namespace std;
class student
{
int rno;
char name[50];
double fee;
public:
student()
{
cout<<"Enter the RollNo:";
cin>>rno;
cout<<"Enter the Name:";
cin>>name;
cout<<"Enter the Fee:";
cin>>fee;
}
void display()
{
cout<<endl<<rno<<"\t"<<name<<"\t"<<fee;
}
};

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

You might also like