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

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)

The document contains multiple C++ programs demonstrating various programming concepts such as class and object definition, inline functions, default arguments, function overloading, call by reference, return by reference, parameterized constructors, operator overloading, and different types of inheritance. Each program is accompanied by its output, showcasing the results of the implemented concepts. The examples serve as practical illustrations for learning C++ programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

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)

The document contains multiple C++ programs demonstrating various programming concepts such as class and object definition, inline functions, default arguments, function overloading, call by reference, return by reference, parameterized constructors, operator overloading, and different types of inheritance. Each program is accompanied by its output, showcasing the results of the implemented concepts. The examples serve as practical illustrations for learning C++ programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

1.

write a program in c++ for defining class and object

#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;
}

// Method to display the car's details


void displayDetails() {
cout << "Car Brand: " << brand << endl;
cout << "Manufacturing Year: " << year << endl;
}
};

// Main function
int main() {
// Creating an object of the Car class
Car car1;

// Setting details for car1


car1.setDetails("Toyota", 2020);

// Displaying car1's details


car1.displayDetails();

return 0;
}

Car Brand: Toyota


Manufacturing Year: 2020
2.write a program to implement the concept of inline function

#include<iostream>
using namespace std;

// Define an inline function to calculate the square of a number


inline int square(int x) {
return x * x;
}

int main() {
int num;

cout << "Enter a number: ";


cin >> num;

// Calling the inline function


cout << "Square of " << num << " is: " << square(num) << endl;

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;

// Function with default arguments


// 'b' has a default value of 10, and 'c' has a default value of 20
int add(int a, int b = 10, int c = 20) {
return a + b + c;
}

int main() {
// Calling the function with all arguments
cout << "Sum (3, 5, 7): " << add(3, 5, 7) << endl;

// Calling the function with only two arguments, uses default


value for 'c'
cout << "Sum (3, 5): " << add(3, 5) << endl;

// Calling the function with only one argument, uses default


values for 'b' and 'c'
cout << "Sum (3): " << add(3) << endl;

return 0;
}

output :

Sum (3, 5, 7): 15


Sum (3, 5): 28
Sum (3): 33
4.write a program to implement the concept of function overloading

#include<iostream>
using namespace std;

// Function to add two integers


int add(int a, int b) {
return a + b;
}

// Function to add three integers


int add(int a, int b, int c) {
return a + b + c;
}

// Function to add two floating-point numbers


float add(float a, float b) {
return a + b;
}

int main() {
// Calling the add function with two integers
cout << "Sum of 3 and 5 (int): " << add(3, 5) << endl;

// Calling the add function with three integers


cout << "Sum of 1, 2, and 3 (int): " << add(1, 2, 3) << endl;

// Calling the add function with two floating-point numbers


cout << "Sum of 2.5 and 3.5 (float): " << add(2.5f, 3.5f) << endl;

return 0;
}

output :

Sum of 3 and 5 (int): 8


Sum of 1, 2, and 3 (int): 6
Sum of 2.5 and 3.5 (float): 6
5.write a program to implement the concept of call by reference

#include<iostream>
using namespace std;

// Function to swap two numbers using call by reference


void swap(int &a, int &b) {
int temp = a;
a = b;
b = temp;
}

int main() {
int num1 = 10, num2 = 20;

cout << "Before swapping:" << endl;


cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

// Call the swap function with references to num1 and num2


swap(num1, num2);

cout << "After swapping:" << endl;


cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

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;

// Function to return reference to the larger of two variables


int& larger(int &a, int &b) {
if (a > b)
return a; // Return a reference to 'a'
else
return b; // Return a reference to 'b'
}

int main() {
int x = 10, y = 20;

// Get a reference to the larger of x and y


int &largerVal = larger(x, y);

cout << "Larger value: " << largerVal << endl;

// Modify the larger value


largerVal += 10;

cout << "Modified x: " << x << endl;


cout << "Modified y: " << y << endl;

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;
}

// Function to calculate and return the area of the rectangle


int area() {
return length * breadth;
}
};

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

// Displaying the area of the rectangles


cout << "Area of rect1: " << rect1.area() << endl;
cout << "Area of rect2: " << rect2.area() << endl;

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) {}

// Overloading the binary '+' operator


Complex operator + (const Complex &obj) {
Complex temp;
temp.real = real + obj.real;
temp.imag = imag + obj.imag;
return temp;
}

// Function to display the complex number


void display() {
cout << real << " + " << imag << "i" << endl;
}
};

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;

// Displaying the result


cout << "Sum of complex numbers: ";
c3.display();

return 0;
}

output :

Sum of complex numbers: 4.8 + 7.2i


9.write a program to implement the concept of single inheritance

#include<iostream>
using namespace std;

// Base class (Parent class)


class Animal {
public:
void eat() {
cout << "This animal is eating." << endl;
}
};

// Derived class (Child class) inheriting from the base class


class Dog : public Animal {
public:
void bark() {
cout << "The dog is barking." << endl;
}
};

int main() {
// Creating an object of the derived class (Dog)
Dog myDog;

// Calling the base class method using the derived class object
myDog.eat();

// Calling the derived class method


myDog.bark();

return 0;
}

output :

This animal is eating.


The dog is barking.
10.write a program to implement the concept of multilevel 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, inheriting from Dog


class Puppy : public Dog {
public:
void weep() {
cout << "The puppy is weeping." << endl;
}
};

int main() {
// Creating an object of the Puppy class
Puppy myPuppy;

// Calling methods from the base and derived classes


myPuppy.eat(); // From Animal
myPuppy.bark(); // From Dog
myPuppy.weep(); // From Puppy

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;
}
};

// Derived class inheriting from both Printer and Scanner


class MultiFunctionPrinter : public Printer, public Scanner {
public:
void copy() {
cout << "Copying from MultiFunctionPrinter 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;

// Calling methods from the base class and derived classes


myDog.eat(); // From Animal
myDog.bark(); // From Dog

myCat.eat(); // From Animal


myCat.meow(); // From Cat

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;
}
};

// Derived class 1 inheriting from Vehicle


class Car : public Vehicle {
public:
void drive() {
cout << "Car is driving." << endl;
}
};

// Derived class 2 inheriting from Engine


class Truck : public Engine {
public:
void load() {
cout << "Truck is loading." << endl;
}
};

// Derived class 3 inheriting from both Car and Truck (Hybrid)


class HybridVehicle : public Car, public Truck {
public:
void display() {
cout << "This is a Hybrid Vehicle." << endl;
}
};

int main() {
HybridVehicle hv;

// Calling methods from the base classes and derived classes


hv.start(); // From Vehicle
hv.run(); // From Engine
hv.drive(); // From Car
hv.load(); // From Truck
hv.display(); // From HybridVehicle

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;
}

// Writing data to the file


outFile << "Hello, World!" << endl;
outFile << "This is a file handling example in C++." << endl;
outFile << "File handling allows you to read and write files." << endl;

// Close the output file


outFile.close();
cout << "Data written to file successfully." << endl;

// Reading from a file


ifstream inFile("example.txt"); // Open the file for reading
if (!inFile) {
cerr << "Error opening file!" << endl;
return 1;
}

string line;
cout << "Reading from file:" << endl;

// Read and display data from the file


while (getline(inFile, line)) {
cout << line << endl; // Print each line
}

// Close the input file


inFile.close();

return 0;
}

output :

Data written to file successfully.


Reading from file:
Hello, World!
This is a file handling example in C++.
File handling allows you to read and write files.
15. write a program to implement the concept of overloading unary operator

#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;
}

// Overloading the unary '-' operator


void operator -() {
value = -value; // Negate the value
}

// Function to display the value


void display() {
cout << "Value: " << value << endl;
}
};

int main() {
// Creating an object of the Number class
Number num(10);

// Display the initial value


cout << "Before negation: ";
num.display();

// Using the overloaded unary '-' operator


-num; // Negates the value

// Display the negated value


cout << "After negation: ";
num.display();

return 0;
}

outpur :

Before negation: Value: 10


After negation: Value: -10

You might also like