C++ Practical file
C++ Practical file
Output Of Program:
P a g e 1 | 17
2. A.Program to show the importance of break :-
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
cout<<"Program to show importance of break\n";
for(int i=1; i<=10; i++)
{
cout<<i<<" ";
if(i==5)
{
cout<<"\nBye";
break;
}
}
return 0;
}
Output of program:
P a g e 2 | 17
B. Program to show the importance of continue statement :-
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
cout<<"Program to show importance of break\n";
for(int i=1; i<=10; i++)
{
cout<<i<<" ";
if(i==5)
{
cout<<"\nBye";
break;
}
}
return 0;
}
Output Of Program:
P a g e 3 | 17
3. Program to show how pass by value works:
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a,b;
void swap(int,int);
cout<<"Enter two numbers =";
cin>>a>>b;
cout<<"\n Before calling (in main) a = "<<a<<" b = "<<b;
swap(a,b);
cout<<"\n After calling (in main) a = "<<a<<" b= "<<b;
return 0;
}
void swap(int x, int y)
{
int z;
z=x;
x=y;
y=z;
cout<<"\n After modification (in function) x = "<<x<<" y = "<<y;
}
P a g e 4 | 17
4. Program to show how pass by address works:
#include<iostream>
void modifyValue(int *ptr);
int main()
{
int num = 10;
std::cout << "Original value: " << num << std::endl;
Output Of Program:
P a g e 5 | 17
5. Program to show concept of classes and objects:
#include <iostream>
#include <string>
class Person
{
public:
std::string name;
int age;
void displayInfo()
{std::cout << "Name: " << name << ", Age: " << age << " years" << std::endl; }
};
int main()
{
Person person1;
person1.name = "Nikhil";
person1.age = 21;
person1.displayInfo();
Person person2;
person2.name = "Ashok";
person2.age = 40;
person2.displayInfo();
return 0;
}
Output of a program:
P a g e 6 | 17
6. Program to show working of constructor:
#include <iostream>
class MyClass
{
public:
// Constructor
MyClass() {
std::cout << "Constructor called!" << std::endl;
}
void displayMessage() {
std::cout << "Hello from MyClass!" << std::endl;
}
};
int main()
{
MyClass myObject;
myObject.displayMessage();
return 0;
}
Output Of Program:
P a g e 7 | 17
7. Program to show scope resolution operator:
#include <iostream>
int globalVar = 10;
class MyClass
{
public:
int classVar = 20;
void displayValues() {
std::cout << "Global Variable: " << ::globalVar << std::endl;
Output Of Program:
P a g e 8 | 17
8. Program to show how a data is entered and displayed using structure:
#include <iostream>
#include <string>
struct Student
{
std::string name;
int age;
double gpa;
};
int main()
{
const int MAX_STUDENTS = 3;
Student students[MAX_STUDENTS];
for (int i = 0; i < MAX_STUDENTS; ++i)
{
std::cout << "Enter details for Student " << i + 1 << ":" << std::endl;
std::cout << "Name: ";
std::getline(std::cin, students[i].name);
std::cout << "Age: ";
std::cin >> students[i].age;
std::cout << "GPA: ";
std::cin >> students[i].gpa;
}
std::cout << "\nDetails of Students:\n";
for (int i = 0; i < MAX_STUDENTS; ++i) {
std::cout << "Student " << i + 1 << ":\n";
std::cout << "Name: " << students[i].name << std::endl;
std::cout << "Age: " << students[i].age << std::endl;
std::cout << "GPA: " << students[i].gpa << std::endl;
P a g e 9 | 17
std::cout << "---------------------\n";
}
return 0;
}
Output Of Program:
P a g e 10 | 17
9. Program to passing object by value:
#include <iostream>
class MyClass
{ public:
int data;
MyClass(int val) : data(val) {}
void display()
{
std::cout << "Data: " << data << std::endl;
}
};
void passObjectByValue(MyClass obj)
{ std::cout << "Inside passObjectByValue function:" << std::endl;
obj.display();
obj.data = 999;
} int main()
{ MyClass myObject(42);
std::cout << "Original data before function call:" << std::endl;
myObject.display();
passObjectByValue(myObject);
std::cout << "Original data after function call:" << std::endl;
myObject.display();
return 0; }
Output Of Program:
P a g e 11 | 17
10. Program to use the show of friend function:
#include <iostream>
using namespace std;
class GFG
{private:
int private_variable;
protected:
int protected_variable;
public:
GFG()
{private_variable = 10;
protected_variable = 99;
}friend class F;
};class F {
public:
void display(GFG& t)
{ cout << "The value of Private Variable = "
<< t.private_variable << endl;
cout << "The value of Protected Variable = "
<< t.protected_variable;
}
};int main()
{ GFG g;
F fri;
fri.display(g);
return 0;}
Output Of Program:
P a g e 12 | 17
11.Program to use inline function:
#include <iostream>
inline int add(int a, int b)
{
return a + b;
}
int main() {
int num1 = 5;
int num2 = 7;
int result = add(num1, num2);
std::cout << "Result of adding " << num1 << " and " << num2 << ": " << result << std::endl;
return 0;
}
Output Of Program:
P a g e 13 | 17
12. Program of single inheritance:
#include <iostream>
class Animal {
public:
void eat() {
std::cout << "Animal is eating." << std::endl;
}
void sleep() {
std::cout << "Animal is sleeping." << std::endl;
}
};
class Dog : public Animal {
public:
void bark() {
std::cout << "Dog is barking." << std::endl;
}
};
int main() {
Dog myDog;
myDog.eat();
myDog.sleep();
myDog.bark();
return 0;
}
P a g e 14 | 17
13. Program to show the use of function overloading:
#include <iostream>
int add(int a, int b) {
std::cout << "Adding two integers: ";
return a + b;
}
int add(int a, int b, int c) {
std::cout << "Adding three integers: ";
return a + b + c;
}
std::string add(const std::string& str1, const std::string& str2) {
std::cout << "Concatenating two strings: ";
return str1 + str2;
}
int main() {
int sum1 = add(3, 5);
std::cout << sum1 << std::endl;
int sum2 = add(1, 4, 6);
std::cout << sum2 << std::endl;
std::string result = add("Hello, ", "World!");
std::cout << result << std::endl;
return 0;
}
Output Of Program:
P a g e 15 | 17
14. Program to show the use of Operator Overloading:
#include <iostream>
class Complex {
private:
double real;
double imag;
public:
Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}
Complex operator+(const Complex& other) const {
return Complex(real + other.real, imag + other.imag);
}
// Overloading the - operator for subtraction
Complex operator-(const Complex& other) const {
return Complex(real - other.real, imag - other.imag);
}
// Overloading the * operator for multiplication
Complex operator*(const Complex& other) const {
return Complex((real * other.real) - (imag * other.imag),
(real * other.imag) + (imag * other.real));
}
// Overloading the << operator for output
friend std::ostream& operator<<(std::ostream& os, const Complex& complex) {
os << "(" << complex.real << " + " << complex.imag << "i)";
return os;
}
};
int main()
{
// Create two complex numbers
P a g e 16 | 17
Complex c1(2.0, 3.0);
Complex c2(1.5, 2.5);
Output Of Program:
P a g e 17 | 17