CPP LABPGM 1- 12
CPP LABPGM 1- 12
#include <iostream.h>
#include<conio.h>
class complexno
{
private:
int real,img;
public:
complexno()
{
real=0;
img=0;
}
void get()
{
cout<<"\nreal: ";
cin>>real;
cout<<"imaginary: ";
cin>>img;
}
void disp()
{
cout<<real<<"+i"<<img;
}
complexno add(complexno c1,complexno c2)
{
complexno c3;
c3.real = c1.real+c2.real;
c3.img=c1.img+c2.img;
return c3;
}
complexno sub(complexno c1,complexno c2)
{
complexno c3;
c3.real = c1.real-c2.real;
c3.img=c1.img-c2.img;
return c3;
}
complexno multiply(complexno c1,complexno c2)
{
complexno c3;
c3.real = (c1.real*c2.real)-(c1.img*c2.img);
c3.img=(c1.real*c2.img)+(c1.img*c2.real);
return c3;
}
complexno scalar(complexno c1,int n1)
{
complexno c3;
c3.real = c1.real*n1;
c3.img=c1.img*n1;
return c3;
}
};
void main()
{
cout<<"Complex Numbers";
int n;
complexno c1,c2,c3;
cout<<"\nFirst Complex Number";
c1.get();
cout<<"\nSecond Complex Number";
c2.get();
c3=c3.add(c1,c2);
cout<<"\nFirst Complex Number: ";
c1.disp();
cout<<"\nSecond Complex Number: ";
c2.disp();
cout<<"\nAddition of 2 Complex Numbers: ";
c3.disp();
c3=c3.sub(c1,c2);
cout<<"\nSubtraction of 2 Complex Numbers: ";
c3.disp();
c3=c3.multiply(c1,c2);
cout<<"\nMultiplication of 2 Complex Numbers: ";
c3.disp();
cout<<"\nScalar Multiplication";
cout<<"\n n= ";
cin>>n;
c3=c3.scalar(c1,n);
cout<<"\n Scalar Multiplication of first complex no: ";
c3.disp();
}
//CPP2 LABPGM2 2D-point
#include <iostream>
#include<math.h>
using namespace std;
class point2d
{
public:
int x,y;
point2d()
{
x=0;
y=0;
}
void get()
{
cout<<"\nx: ";
cin>>x;
cout<<"y: ";
cin>>y;
}
void disp()
{
cout<<"("<<x<<" , "<<y<<")";
}
float dist(point2d p2)
{
return sqrt(pow(p2.x - x, 2) + pow(p2.y - y, 2) * 1.0);
}
void equ(point2d p2)
{
if((x==p2.x)&&(y==p2.y))
cout<<"\nBoth the points are Equal";
else
cout<<"\nBoth the points are not Equal";
}
};
int main()
{
cout<<"\nPoint 2D";
int n;
point2d p1,p2;
cout<<"\nFirst Coordinates";
p1.get();
cout<<"\nSecond Coordinates";
p2.get();
return 0;
}
// CPP LABPRGM 3
//Harmonic Progression
#include <iostream>
using namespace std;
class hp
{
public:
int a,n,d;
void get()
{
cout<<"\nFirst Term: ";
cin>>a;
cout<<"n: ";
cin>>n;
cout<<"Difference: ";
cin>>d;
}
void disp(){
for(float i = 1; i <= n; i++){
cout<<"1/"<<(float)(a+(i-1)*d);
if(i!=n)cout<<"+";
}
}
float findSeriesSum(){
float sumVal = 0;
float term = 0;
for(float i = 1; i <= n; i++){
term = (1.0)/(float)(a + (i-1)*d);
sumVal += term;
}
return sumVal;
}
};
int main(){
hp h1;
h1.get();
h1.disp();
cout<<"\nThe sum of HP is "<<h1.findSeriesSum();
return 0;
}
//CPP5 LABPRGM 5
//time program
#include <iostream>
};
int main()
{
cout<<"Time Implementation";
hour h1,h2,h3;
h1.get();
cout<<"First Time: ";
h1.disp();
h2.get();
cout<<"Second Time: ";
h2.disp();
h3=h3.diff(h1,h2);
cout<<"\nTime Difference: ";
h3.disp();
h1.add();
h1.disp();
int secs=h1.conv();
cout<<"\nTotal Seconds: "<<secs;
return 0;
}
#include <iostream>
class Matrix3x3 {
private:
static int objectCount; // Static member variable to count the number of objects
int matrix[3][3];
public:
// Constructor
Matrix3x3() {
// Increment the object count when a new object is created
++objectCount;
int main() {
// Create two matrix objects
Matrix3x3 matrix1;
Matrix3x3 matrix2;
return 0;
}
public:
//Default Constructor
CString() {
// Initialize the string to an empty string
data[0] = '\0';
}
CString(char* s)
{
strcpy(data,s);
}
// Copy Constructor
CString(const CString& other) {
strcpy(data, other.data);
}
// Copy Constructor
CString str4 = str2;
// Reverse a string
CString reversed = result.reverse();
std::cout << "Reversed string: ";
reversed.display();
return 0;
}
//CPP 9 LABPRGM 9
Run Time Polymorphism
#include<iostream>
using namespace std;
class Shape
{
public: double a,b;
void get_adata ()
{
cin>>a;
}
void get_bdata ()
{
cin>>b;
}
virtual void display_area () = 0;
};
int main()
{
Triangle t;
Shape *st = &t;
cout<<"Enter base and altitude: ";
st->get_adata();
st->get_bdata();
st->display_area();
Rectangle r;
Shape *sr = &r;
cout<<"Enter length and breadth: ";
sr->get_adata();
sr->get_bdata();
sr->display_area();
circle c;
Shape *sc = &c;
cout<<"Enter radius ";
sc->get_adata();
sc->display_area();
return 0;
}
public:
// Constructor
DynamicArray(size_t initialSize) : size(initialSize) {
if (initialSize > 0) {
arr = new T[size];
} else {
arr = nullptr;
}
}
// Destructor
~DynamicArray() {
delete[] arr;
}
int main() {
try {
// Create a dynamic array of integers
DynamicArray<int> intArray(5);
return 0;
}
int main() {
// Declare a vector to store integers
vector<int> numbers;
return 0;
}
struct Contact {
string name;
string phoneNumber;
};
if (file.is_open()) {
file << contact.name << "," << contact.phoneNumber << endl;
file.close();
cout << "Contact added successfully!" << endl;
} else {
cerr << "Unable to open file for writing." << endl;
}
}
void displayAllContactsFromFile() {
ifstream file("telephone_directory.txt");
if (file.is_open()) {
string line;
cout << "Telephone Directory:" << endl;
cout << "Name: " << name << "\tPhone Number: " << phoneNumber << endl;
}
file.close();
} else {
cerr << "Unable to open file for reading." << endl;
}
}
if (file.is_open()) {
string line;
bool found = false;
if (contactName == name) {
found = true;
string phoneNumber = line.substr(commaPos + 1);
cout << "Contact found:" << endl;
cout << "Name: " << contactName << "\tPhone Number: " << phoneNumber << endl;
break;
}
}
if (!found) {
cout << "Contact not found." << endl;
}
file.close();
} else {
cerr << "Unable to open file for reading." << endl;
}
}
int main() {
int choice;
Contact newContact;
do {
cout << "Telephone Directory Menu:" << endl;
cout << "1. Add a new contact" << endl;
cout << "2. Display all contacts" << endl;
cout << "3. Search for a contact" << endl;
cout << "4. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter name: ";
cin.ignore();
getline(cin, newContact.name);
cout << "Enter phone number: ";
getline(cin, newContact.phoneNumber);
addContactToFile(newContact);
break;
case 2:
displayAllContactsFromFile();
break;
case 3:
cout << "Enter name to search: ";
cin.ignore();
getline(cin, newContact.name);
searchContact(newContact.name);
break;
case 4:
cout << "Exiting the program. Goodbye!" << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
return 0;
}