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

C++ Lab

The document contains 10 code snippets demonstrating various C++ programming concepts: 1. A program to calculate the sum of digits of a number 2. A program demonstrating use of constructors 3. A program overloading the binary + operator for a Complex class 4. A program demonstrating multilevel inheritance 5. A program demonstrating function overloading 6. A program using default arguments in a function 7. A program using an array of objects 8. A program handling exceptions 9. A program performing formatted output 10. A program copying content from one text file to another

Uploaded by

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

C++ Lab

The document contains 10 code snippets demonstrating various C++ programming concepts: 1. A program to calculate the sum of digits of a number 2. A program demonstrating use of constructors 3. A program overloading the binary + operator for a Complex class 4. A program demonstrating multilevel inheritance 5. A program demonstrating function overloading 6. A program using default arguments in a function 7. A program using an array of objects 8. A program handling exceptions 9. A program performing formatted output 10. A program copying content from one text file to another

Uploaded by

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

/*1.

write C++ program to find sum of digits of a given number*/


#include <iostream.h>
#include <conio.h>

void main()
{
clrscr();

int number, digit, sum = 0;

cout << "Enter a number: ";


cin >> number;

while(number != 0)
{
digit = number % 10;
sum += digit;
number /= 10;
}

cout << "Sum of digits: " << sum;

getch();
}

Output:

Enter a number: 12345


Sum of digits: 15
/*2. write a c++ program to demonstrate use of constructor*/

#include <iostream.h>
#include <conio.h>

class MyClass {
private:
int myNumber;

public:
MyClass() {
myNumber = 0;
cout << "Default Constructor Called..!!\n";
}

MyClass(int num) {
myNumber = num;
cout << "Parameterized Constructor Called..!!\n";
}

void display() {
cout << "My number is " << myNumber << endl;
}
};

void main() {
clrscr();

MyClass obj1; // Call default constructor


obj1.display();

MyClass obj2(10); // Call parameterized constructor


obj2.display();

getch();
}

Output:

Default Constructor Called..!!


My number is 0
Parameterized Constructor Called..!!
My number is 10
/*3. write a c++ program to perform Overloading of a binary operator*/

#include <iostream.h>
#include <conio.h>
class Complex {
private:
int real;
int imag;
public:
Complex() {
real = 0;
imag = 0;
}
Complex(int r, int i) {
real = r;
imag = i;
}
Complex operator+(Complex c) {
Complex temp;
temp.real = real + c.real;
temp.imag = imag + c.imag;
return temp;
}
Complex operator-(Complex c) {
Complex temp;
temp.real = real - c.real;
temp.imag = imag - c.imag;
return temp;
}
void display() {
cout << real << " + " << imag << "i" << endl;
}
void display1(){
cout<<real<<"-"<<imag<<"i"<<endl;
}
};
void main() {
clrscr();
Complex c1(10, 3);
Complex c2(8, 2);
Complex c3 = c1 + c2;
Complex c4 = c1 - c2;
c1.display();
c2.display();
c3.display();
c4.display1();
getch();
Output:

10 + 3i
8 + 2i
18 + 5i
2-1i
/*4. Write a OOP program to demonstrate the importance of Multilevel Inheritance*/
#include <iostream.h>
#include <conio.h>
class Turbo {
public:
void turbocharge() {
cout << "Turbo is charging..." << endl;
}
};
class Car : public Turbo {
public:
void drive() {
cout << "Car is driving..." << endl;
}
};
class SportsCar : public Car {
public:
void race() {
cout << "Sports car is racing..." << endl;
}
};
int main() {
SportsCar myCar;
myCar.turbocharge(); // call inherited function from Turbo class
myCar.drive(); // call function from Car class
myCar.race(); // call function from SportsCar class

getch(); // wait for a key press before exiting


return 0;
}

Output:

Turbo is charging...
Car is driving...
Sports car is racing...
/* 5. Write a OOP program to demonstrate the Function Overloading*/
#include <iostream.h>
#include <conio.h>
int sum(int a, int b) {
return a + b;
}
float sum(float a, float b) {
return a + b;
}
int main() {
int x, y;
float p, q;0
cout << "Enter two integers: ";
cin >> x >> y;
cout << "Sum of " << x << " and " << y << " is " << sum(x, y) << endl;
cout << "Enter two floats: ";
cin >> p >> q;
cout << "Sum of " << p << " and " << q << " is " << sum(p, q) << endl;
getch();
return 0;
}
Output:
Enter two integers: 26 34
Sum of 26 and 34 is 60
Enter two floats: 2.7 3.9
Sum of 2.7 and 3.9 is 6.6
/*6.write a C++ program to find the sum of given variables using function with default arguments*/
#include <iostream.h>
#include <conio.h>
int sum(int a, int b = 0, int c = 0, int d = 0) {
return a + b + c + d;
}
int main() {
int a, b, c, d;
clrscr();
cout << "Enter 4 numbers: ";
cin >> a >> b >> c >> d;
cout << "Sum of the given numbers is: " << sum(a, b, c, d) << endl;
cout << "Sum of the first two numbers is: " << sum(a, b) << endl;
cout << "Sum of the first three numbers is: " << sum(a, b, c) << endl;
cout << "Sum of the first four numbers is: " << sum(a, b, c, d) << endl;
getch();
return 0;
}
Output:
Enter 4 numbers: 2 3 4 6
Sum of the given numbers is: 15
Sum of the first two numbers is: 5
Sum of the first three numbers is: 9
Sum of the first four numbers is: 15
/*7. Write a C++ program to demonstrate the array of Object*/
#include <iostream.h>
#include <conio.h>
void printSum(int x, int y = 0, int z = 0) {
int sum = x + y + z;
cout << "Sum = " << sum << endl;
}
int main() {
int choice, a, b, c;
cout << "Enter the choice:" << endl;
cout << "1. Add 1 number" << endl;
cout << "2. Add 2 numbers" << endl;
cout << "3. Add 3 numbers" << endl;
cin >> choice;
switch (choice) {
case 1:
cout << "Enter a number: ";
cin >> a;
printSum(a);
break;
case 2:
cout << "Enter 2 numbers: ";
cin >> a >> b;
printSum(a, b);
break;
case 3:
cout << "Enter 3 numbers: ";
cin >> a >> b >> c;
printSum(a, b, c);
break;
default:
cout << "Invalid choice" << endl;
}
getch();
return 0;
}
Output:
Enter the choice:
1. Add 1 number
2. Add 2 numbers
3. Add 3 numbers
3
Enter 3 numbers: 10 20 30
Sum = 60
/*8. Write a C++ program to Handle Exception*/
#include <iostream.h>
#include <conio.h>
#include <setjmp.h>
jmp_buf buffer;int divide(int x, int y) {
if (y == 0) {
longjmp(buffer, 1);
}
return x / y;
}
int main() {
int a, b, result;
cout << "Enter two numbers: ";
cin >> a >> b;
if (setjmp(buffer) != 0) {
cerr << "Division by zero error" << endl;
getch();
return 1;
}
result = divide(a, b);
cout << "Result = " << result << endl;
getch();
return 0;
}
Output:
Enter two numbers: 10 0
Division by zero error
/* 9. Write c++ program to perform Formatted Console Operation*/
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
int main() {
int i = 10;
float f = 3.14159;
cout << "i = " << i << endl;
cout << "f = " << f << endl;
// Formatting output
cout << "i = " << setw(5) << i << endl;
cout << "f = " << setprecision(3) << f << endl;
getch();
return 0;
}
Output:
i = 10
f = 3.14159
i = 10
f = 3.142
/*10. Write a C++ program to copynthe content of one text file into another text*/
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include <dos.h>
void main()
{
clrscr();
char sourceFile[50], destinationFile[50], ch;
cout << "Enter the name of the source file: ";
cin >> sourceFile;
cout << "Enter the name of the destination file: ";
cin >> destinationFile;
ifstream fin(sourceFile, ios::in|ios::binary);
if(!fin)
{
cout << "Error in opening the source file..!!";
getch();
exit(0);
}
ofstream fout(destinationFile, ios::out|ios::binary);
if(!fout)
{
cout << "Error in opening the destination file..!!";
getch();
exit(0);
}
while(!fin.eof())
{
fin.get(ch);
fout.put(ch);
}
fin.close();
fout.close();
cout << "\n\nFile Copied Successfully..!!";
getch();
}
Output:

Enter the name of the source file: file1.txt


Enter the name of the destination file: file2.txt

File Copied Successfully..!!

You might also like