C++ Practical Programs
C++ Practical Programs
CODING:
#include<iostream>
using namespace std;
class Complex
{
public:
int real, imag, scalar;
void setvalue()
{
cin>>real;
cin>>imag;
}
void display()
{
cout<<real<<"+"<<imag<<"i"<<endl;
}
void sum(Complex c1, Complex c2)
{
real=c1.real+c2.real;
imag=c1.imag+c2.imag;
}
OUTPUT:
PROGRAM #2 2-D POINT CLASS
CODING:
#include <iostream>
#include <cmath>
using namespace std;
class point {
public:
double x , y;
double setvalue()
{
cout<<"Enter two points:";
cin>>x>>y;
}
void showvalue()
{
cout<<"The points are: "<<x<<" and "<<y<<endl;
}
void check(point& other)
{
if(x==other.x && y==other.y) cout<<"The given points are equal";
else
cout<<"The given points are not equal";
}
double distanceTo(point& other)
{
return sqrt((x-other.x)*(x-other.x) + (y-other.y)*(y-other.y));
}
};
int main()
{
point p1,p2;
p1.setvalue();
p2.setvalue();
p1.showvalue();
p2.showvalue();
p2.check(p1);
cout << "\nThe distance betwen 2d points p1 and p2 is " << p1.distanceTo(p2)
<< endl;
}
OUTPUT:
PROGRAM #3 HARMONIC PROGRESSION (HP)
CODING:
#include <iostream>
#include <vector>
using namespace std;
class ArithmeticProgression; // Forward declaration
class HarmonicProgression {
private:
int n; // Number of terms
vector<double> terms;
public:
HarmonicProgression(int n) : n(n) {}
cout << "Sum of HP up to " << n << " terms: " << hp.sumToN() << endl;
cout << "Sum of HP to infinity: " << hp.sumToInfinity() << endl;
// Generating corresponding AP
ArithmeticProgression ap(hp);
ap.displayAP();
return 0;
}
OUTPUT:
PROGRAM #4 CALCULATE VOLUME & SURFACE AREA
FOR DIFFERENT SOLIDS
CODING:
#include <iostream>
using namespace std;
const float pi=3.14;
class solid
{
public:
int shape;
float vol, area, radius, height, side;
void chooseshape()
{
cout<<"Choose the Solid to which you want to find volume & Area: \n 1. Sphere\n 2. Cube
\n3. Cylinder\n";
cin>>shape;
}
void volume()
{
switch(shape)
{
case 1:
cout << "Enter the radius of Sphere : ";
cin >> radius;
vol = pi * radius * radius * radius * 4 / 3;
cout<<"Volume of Sphere is "<<vol;
break;
case 2:
cout << "Enter Cube's side :";
cin >> side;
vol = side * side * side;
cout<<"The Volume of Cube is "<<vol;
break;
case 3:
cout << "Enter Cylinder's radius : ";
cin >> radius;
cout << "Enter Cylinder's height : ";
cin >> height;
vol = pi * radius * radius * height;
cout<<"The Volume of Cylinder is "<<vol;
break;
default: cout<<"\nInvalid Choice";
}
}
void surfacearea()
{
switch(shape)
{
case 1:
area = 4 * pi * radius * radius;
cout<<"\nThe Surface Area of Sphere is "<<area;
break;
case 2:
area = 6 * side * side;
cout<<"\nThe Surface Area of Cube is "<<area;
break;
case 3:
area = (2*pi *radius*radius) + (2*pi*radius*height);
cout<<"\nThe Surface Area of Cylinder is "<<area;
break;
default: cout<<"\nInvalid Choice";
}
}
};
int main()
{
solid s;
s.chooseshape();
s.volume();
s.surfacearea();
}
OUTPUT:
PROGRAM #5 TIME MANIPULATION
CODING:
OUTPUT:
PROGRAM #6 MATRIX MANIPULATION
CODING:
#include<iostream>
using namespace std;
class Matrix
{
static int count;
int a[3][3];
public:
Matrix()
{
count++;
}
static int totalObjects(void)
{
return count;
}
void accept();
void display();
void operator +(Matrix x);
void operator *(Matrix x);
};
int Matrix::count = 0;
void Matrix::accept()
{
cout<<"\n Enter Matrix Element (3 X 3) : \n";
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
cout<<" ";
cin>>a[i][j];
}}}
void Matrix::display()
{
for(int i=0; i<3; i++)
{
cout<<" ";
for(int j=0; j<3; j++)
{
cout<<a[i][j]<<"\t";
}
cout<<"\n";
}}
void Matrix::operator +(Matrix x)
{
int mat[3][3];
for(int i=0; i<3; i++)
{
for(int j=0; j<3; j++)
{
mat[i][j]=a[i][j]+x.a[i][j];
}}
cout<<"\n Addition of Matrix : \n\n";
OUTPUT:
PROGRAM #7 STRING MANIPULATION
CODING:
#include <iostream>
#include <cstring>
using namespace std;
class cString
{
private:
static const int MAX_LENGTH = 100;
char str[MAX_LENGTH + 1]; // +1 for null terminator public:
// Default Constructor
cString()
{
str[0] = '\0'; // Initialize as an empty string
}
OUTPUT:
PROGRAM #8 STRING MANIPULATION WITH
DYNAMIC MEMORY ALLOCATION
CODING:
#include <iostream>
#include <cstring>
using namespace std;
class cString {
private:
char* str;
public:
// Constructor
cString(const char* s = "") {
int len = strlen(s);
str = new char[len + 1];
strcpy(str, s);
}
// Copy Constructor
cString(const cString& other) {
int len = strlen(other.str);
str = new char[len + 1];
strcpy(str, other.str);
}
// Destructor
~cString() {
delete[] str;
}
// Concatenate two strings
cString concatenate(const cString& other) const {
int len1 = strlen(str);
int len2 = strlen(other.str);
char* newStr = new char[len1 + len2 + 1];
strcpy(newStr, str);
strcat(newStr, other.str);
cString result(newStr);
delete[] newStr;
return result;
}
// Find the length of the string
int length() const {
return strlen(str);
}
// Reversing a string
cString reverse() const {
int len = strlen(str);
char* reversedStr = new char[len + 1];
for (int i = 0; i < len; ++i) {
reversedStr[i] = str[len - i - 1];
}
reversedStr[len] = '\0';
cString result(reversedStr);
delete[] reversedStr;
return result;
}
// Comparing two strings
int compare(const cString& other) const {
return strcmp(str, other.str);
}
// Display the string
void display() const {
cout << str << endl;
}
};
int main() {
cString s1("Hello");
cString s2("World");
cout << "Comparison of s1 and s2: " << s1.compare(s2) << endl;
return 0;
}
OUTPUT:
PROGRAM #9 AREA OF FIGURES USING
RUN-TIME POLYMORPHISM
CODING:
#include<iostream>
using namespace std;
class Shape
{
public: double a,b;
void get_data ()
{
cin>>a>>b;
}
virtual void display_area () = 0;
};
class Triangle:public Shape
{
public: void display_area ()
{
cout<<"Area of triangle "<<0.5*a*b<<endl;
}
};
class Rectangle:public Shape
{
OUTPUT:
PROGRAM # 10 SORTING AN ARRAY
CODING:
#include <iostream>
#include <stdexcept>
using namespace std;
template<typename T>
class Array {
private:
T* arr;
int size;
public:
// Constructor
Array(int sz) : size(sz) {
arr = new T[size];
}
// Destructor
~Array() {
delete[] arr;
}
// Function to access array elements with bounds checking
T& operator[](int index) {
if (index < 0 || index >= size) {
throw out_of_range("Index out of range");
}
return arr[index];
}
// Function to sort array elements
void sort() {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
T temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
};
int main() {
try {
Array<int> intArray(5);
cout << "Enter 5 integer elements: ";
for (int i = 0; i < 5; ++i) {
cin >> intArray[i];
}
cout << "Array before sorting: ";
for (int i = 0; i < 5; ++i) {
cout << intArray[i] << " ";
}
cout << endl;
intArray.sort();
OUTPUT:
PROGRAM #11 VECTOR STL CONTAINER
CODING:
#include <iostream>
#include <vector>
int main() {
// Declaration and initialization of a vector of integers
vector<int> numbers = {1, 2, 3, 4, 5};
return 0;
}
OUTPUT:
PROGRAM # 12 TELEPHONE DIRECTORY USING FILES
CODING:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Contact {
string name;
string phoneNumber;
};
// Function prototypes
void addContact(ofstream& file);
void searchContact(ifstream& file, const string& name);
void displayAllContacts(ifstream& file);
int main() {
ofstream outFile("contacts.txt", ios::app); // File to store contacts
if (!outFile) {
cerr << "Error opening file!" << endl;
return 1;
}
int choice;
do {
cout << "Telephone Directory" << endl;
cout << "1. Add Contact" << endl;
cout << "2. Search Contact" << endl;
cout << "3. Display All Contacts" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addContact(outFile);
break;
case 2:
{
ifstream inFile("contacts.txt");
if (!inFile) {
cerr << "Error opening file!" << endl;
return 1;
}
string name;
cout << "Enter name to search: ";
cin >> name;
searchContact(inFile, name);
inFile.close();
}
break;
case 3:
{
ifstream inFile("contacts.txt");
if (!inFile) {
cerr << "Error opening file!" << endl;
return 1;
}
displayAllContacts(inFile);
inFile.close();
}
break;
case 4:
cout << "Exiting program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 4);
outFile.close();
return 0;
}
file << contact.name << " " << contact.phoneNumber << endl;
cout << "Contact added successfully." << endl;
}