1.write a program in c++ for defining class and object #includeiostream using namespace std_ Defining a class class Car { Private members (data encapsulation) private string brand_ int year_ Pu (1)
1.write a program in c++ for defining class and object #includeiostream using namespace std_ Defining a class class Car { Private members (data encapsulation) private string brand_ int year_ Pu (1)
#include<iostream>
using namespace std;
// Defining a class
class Car {
// Private members (data encapsulation)
private:
string brand;
int year;
// Public members
public:
// Method to set the car's details
void setDetails(string b, int y) {
brand = b;
year = y;
}
// Main function
int main() {
// Creating an object of the Car class
Car car1;
return 0;
}
#include<iostream>
using namespace std;
int main() {
int num;
return 0;
}
output :
Enter a number: 5
Square of 5 is: 25
3.write a program to implement the concept of default
argument
#include<iostream>
using namespace std;
int main() {
// Calling the function with all arguments
cout << "Sum (3, 5, 7): " << add(3, 5, 7) << endl;
return 0;
}
output :
#include<iostream>
using namespace std;
int main() {
// Calling the add function with two integers
cout << "Sum of 3 and 5 (int): " << add(3, 5) << endl;
return 0;
}
output :
#include<iostream>
using namespace std;
int main() {
int num1 = 10, num2 = 20;
return 0;
}
output :
Before swapping:
num1 = 10, num2 = 20
After swapping:
num1 = 20, num2 = 10
6.write the program to implement the concept of return by reference
#include<iostream>
using namespace std;
int main() {
int x = 10, y = 20;
return 0;
}
output :
Larger value: 20
Modified x: 10
Modified y: 30
7.write a program to implement the concept of parameterised
constructor
#include<iostream>
using namespace std;
class Rectangle {
private:
int length, breadth;
public:
// Parameterized constructor to initialize length and breadth
Rectangle(int l, int b) {
length = l;
breadth = b;
}
int main() {
// Creating objects with the parameterized constructor
Rectangle rect1(10, 5); // Rectangle with length = 10, breadth = 5
Rectangle rect2(7, 3); // Rectangle with length = 7, breadth = 3
return 0;
}
output :
Area of rect1: 50
Area of rect2: 21
8.write a program to implement the concept of overloading binary operator
#include<iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
// Constructor to initialize real and imaginary parts
Complex(float r = 0, float i = 0) : real(r), imag(i) {}
int main() {
// Creating two complex number objects
Complex c1(3.2, 4.5);
Complex c2(1.6, 2.7);
// Adding the two complex numbers using the overloaded '+' operator
Complex c3 = c1 + c2;
return 0;
}
output :
#include<iostream>
using namespace std;
int main() {
// Creating an object of the derived class (Dog)
Dog myDog;
// Calling the base class method using the derived class object
myDog.eat();
return 0;
}
output :
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "This animal is eating." << endl;
}
};
// Derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "The dog is barking." << endl;
}
};
int main() {
// Creating an object of the Puppy class
Puppy myPuppy;
return 0;
}
output :
This animal is eating.
The dog is barking.
The puppy is weeping.
11.write a program to implement the concept of multiple inheritance
#include <iostream>
using namespace std;
// Base class 1
class Printer {
public:
void print() {
cout << "Printing from Printer class." << endl;
}
};
// Base class 2
class Scanner {
public:
void scan() {
cout << "Scanning from Scanner class." << endl;
}
};
int main() {
// Creating an object of the derived class
MultiFunctionPrinter mfp;
// Calling methods from both base classes and the derived class
mfp.print(); // From Printer
mfp.scan(); // From Scanner
mfp.copy(); // From MultiFunctionPrinter
return 0;
}
output :
Printing from Printer class.
Scanning from Scanner class.
Copying from MultiFunctionPrinter class.
12.write a program to implement concept of hierarchical inheritance
#include <iostream>
using namespace std;
// Base class
class Animal {
public:
void eat() {
cout << "This animal is eating." << endl;
}
};
// Derived class 1
class Dog : public Animal {
public:
void bark() {
cout << "The dog is barking." << endl;
}
};
// Derived class 2
class Cat : public Animal {
public:
void meow() {
cout << "The cat is meowing." << endl;
}
};
int main() {
// Creating objects of derived classes
Dog myDog;
Cat myCat;
return 0;
}
output:
This animal is eating.
The dog is barking.
This animal is eating.
The cat is meowing.
13.write a program to implement concept of hybrid inheritance
#include <iostream>
using namespace std;
// Base class 1
class Vehicle {
public:
void start() {
cout << "Vehicle started." << endl;
}
};
// Base class 2
class Engine {
public:
void run() {
cout << "Engine is running." << endl;
}
};
int main() {
HybridVehicle hv;
return 0;
}
output :
Vehicle started.
Engine is running.
Car is driving.
Truck is loading.
This is a Hybrid Vehicle.
14.write a program to implement the concept of file handling
#include <iostream>
#include <fstream> // Include fstream for file handling
using namespace std;
int main() {
// Writing to a file
ofstream outFile("example.txt"); // Create and open a text file
if (!outFile) {
cerr << "Error creating file!" << endl;
return 1;
}
string line;
cout << "Reading from file:" << endl;
return 0;
}
output :
#include<iostream>
using namespace std;
class Number {
private:
int value; // A private data member to store the number
public:
// Constructor to initialize the number
Number(int v = 0) {
value = v;
}
int main() {
// Creating an object of the Number class
Number num(10);
return 0;
}
outpur :