ut.
ut.
Sr. Page.
Particular Date Sign
No No
Write a simple program (without Class) to use of
1
operators in C++.
11 Destructor.
1
Assignment no. 1
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int
a,b;
a=10;
b=5;
cout<<"Value of a="<<a<<endl;
cout<<"Value of b="<<b<<endl;
cout<<"a+b="<< (a+b) <<endl;
cout<<"a-b="<< (a-b) <<endl;
cout<<"a*b="<< (a*b) <<endl;
cout<<"a/b="<< (a/b) <<endl;
cout<<"a%b="<< (a%b) <<endl;
getch();
return 0;
}
2
Assignment no. 2
3
Assignment no. 3
#include<iostream.h>
#include<conio.h>
class Room
{
public:
double
length;
double
breadth;
double
height;
double calculateArea()
{
return length * breadth;
}
double calculateVolume()
{
return length * breadth * height;
}
};
int main()
{
Room room1;
room1.length=42.5;
room1.breadth=30.8;
room1.height=19.2;
cout<<"Length = "<<room1.length<<endl;
4
cout<<"Breadth = "<<room1.breadth<<endl;
cout<<"Height = "<<room1.height<<endl;
cout<<"Area of Room="<< room1.calculateArea() <<endl;
cout<<"Area of Room="<< room1.calculateVolume() <<endl;
getch();
return 0;
}
5
Assignment no.4
6
Assignment no.5
7
Assignment no. 6
8
Assignment no. 7
int main() {
int num;
clrscr();
cout << "Enter a number: ";
cin >> num;
cout << "Square of " << num << " is: " << result << endl;
getch();
return 0;
}
9
Assignment no. 8
10
Assignment no. 9
#include <iostream.h>
#include<conio.h>
protected:
int protectedData;
public:
MyClass(int privateValue, int protectedValue) : privateData(privateValue), protectedData(protectedValue)
{}
int main() {
MyClass obj(10, 20);
clrscr();
// Accessing using friend function
friendFunction(obj);
11
// Accessing using friend class
FriendClass friendObj;
friendObj.accessPrivateAndProtected(obj);
getch();
return 0;
}
12
Assignment no. 10
class MyClass {
private:
int data;
public:
// Default constructor
MyClass() {
data = 0;
cout << "Default Constructor called." << endl;
}
// Parameterized constructor
MyClass(int value) {
data = value;
cout << "Parameterized Constructor called with value: " << value << endl;
}
// Copy constructor
MyClass(const MyClass &other) {
data = other.data;
cout << "Copy Constructor called. Copied data from another object." << endl;
}
13
cout << "Data: " << data << endl;
}
};
int main() {
// Using default constructor
MyClass obj1;
clrscr();
obj1.displayData();
14
Assignment no. 11
Q. Destructor.
#include <iostream.h>
#include<conio.h>
class Employee {
public:
Employee() {
cout<<"Constructor Invoked"<<endl;
}
~Employee() {
cout<<"Destructor Invoked"<<endl;
}
};
int main(void) {
clrscr();
Employee e1;
Employee e2;
getch();
return 0;
}
15
Assignment no. 12
17
Assignment no. 13
class Swimming {
public:
void swim() {
cout << "Swimming in the water." << endl;
18
}
};
19
};
int main() {
clrscr();
// Single Inheritance
Dog myDog;
myDog.eat();
myDog.bark();
// Multiple Inheritance
Duck myDuck;
myDuck.fly();
myDuck.swim();
myDuck.quack();
// Multilevel Inheritance
IntelligentDolphin smartDolphin;
smartDolphin.giveBirth();
smartDolphin.swim();
smartDolphin.solveProblems();
getch();
return 0;
}
20
Assignment no. 14
• Static Polymorphism :
#include <iostream>
class Shape {
public:
// Function Overloading
double calculateArea(int side) {
return side * side; // Area of a square
}
int main() {
Shape shape;
return 0;
}
21
• Dynamic Polymorphism :
#include <iostream>
int main() {
// Dynamic polymorphism through function overriding
Animal* dog = new Dog();
22
Animal* cat = new Cat();
delete dog;
delete cat;
return 0;
}
23
Assignment no. 15
public:
// Constructor
Circle(double r) : radius(r) {}
24
// Implement the pure virtual function
double calculateArea() const override {
return 3.14 * radius * radius; // Area of a circle
}
};
public:
// Constructor
Square(double s) : side(s) {}
public:
25
// Constructor
Triangle(double b, double h) : base(b), height(h) {}
int main() {
// Using dynamic binding for polymorphism
Shape* shapes[3] = {new Circle(5.0), new Square(4.0), new Triangle(3.0, 6.0)};
delete shape;
}
return 0;
}
26