0% found this document useful (0 votes)
31 views13 pages

C ++ All Programs

Uploaded by

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

C ++ All Programs

Uploaded by

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

1st Program

#include<iostream>
using namespace std;
inline int largest(int a , int b , int c);
inline int smallest(int a , int b , int c);
int main() {
int a , b ,c;
cout << "Enter three numbers : ";
cin >> a >> b >> c;
int large = largest(a , b ,c);
cout << "\n Largest of " << a << " , " << b << " , " << c << " is " << large;
int small = smallest(a , b ,c);
cout << "\n Smallest of " << a << " , " << b << " , " << c << " is " << small;
//return 0;

int seclargest = (a + b + c) - (large + small);

cout<<"\nSecond Largest Number is = "<<seclargest<<endl;


}

inline int largest(int a , int b , int c) {


if(a > b && a > c)
return a;
else if(b > c)
return b;
else
return c;
}

inline int smallest(int a , int b , int c) {


if(a < b && a < c)
return a;
else if(b < c)
return b;
else
return c;
}
2nd Program

#include<iostream>
using namespace std;
float vol(int,int);
float vol(float);
int vol(int);

int main()
{
int r,h,a;
float r1;
cout<<"Enter radius and height of a cylinder:";
cin>>r>>h;
cout<<"Enter side of cube:";
cin>>a;
cout<<"Enter radius of sphere: ";
cin>>r1;
cout<<"Volume of cylinder is "<<vol(r,h);
cout<<"\nVolume of cube is "<<vol(a);
cout<<"\nVolume of sphere is "<<vol(r1);
return 0;
}
float vol(int r,int h)
{
return(3.14*r*r*h);
}
float vol(float r1)
{
return((4*3.14*r1*r1*r1)/3);
}
int vol(int a)
{
return(a*a*a);
}
3rd Program

#include<iostream.h>
#include<conio.h>
#define SIZE 10
class emp
{
float m1,m2,m3;
char name[20],num[10];
public:
void getdata();
void dispdata();
};
void emp::getdata()
{
cout<<"\nEnter student USN: " ;
cin>>name;
cout<<"Enter student name: " ;
cin>>num;
cout<<"Enter student's 3 marks: " ;
cin>>m1>>m2>>m3;
}
void emp::dispdata()
{
float avg,low=m3;
if(m2<low)
low=m2;
else if(m3<low)
low=m3;
avg=(m1+m2+m3-low)/2;
cout<<"\n student USN: "<<name
<<"\n student name: "<<num
<<"\n student average: "<<avg;
}
void main()
{
clrscr();
emp ob[SIZE];
int n;
cout<<"\n";
cout<<"\n*******************************"
<<"\n Students Report"
<<"\n*******************************"
<<"\n Enter the number of students: ";
cin>>n;
for(int i=0;i<n;i++)
{
ob[i].getdata();
}
clrscr();
cout<<"\n-----------------"
<<"\nstudents Details::"
<<"\n-----------------";
for( i=0;i<n;i++)
{
cout<<"\n\n student:"<<i+1
<<"\n ----------";
ob[i].dispdata();
}
getch();
}
4th Program

#include <iostream>

const int MAX_ROWS = 100;


const int MAX_COLS = 100;

class Matrix {
private:
int rows, cols;
int data[MAX_ROWS][MAX_COLS];

public:
Matrix(int r, int c) : rows(r), cols(c) {}

void inputMatrix() {
std::cout << "Enter matrix elements:" << std::endl;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
std::cout << "Enter element at position (" << i << ", " << j << "): ";
std::cin >> data[i][j];
}
}
}

friend bool operator==(const Matrix& m1, const Matrix& m2);

Matrix operator+(const Matrix& other) const {


Matrix result(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result.data[i][j] = data[i][j] + other.data[i][j];
}
}
return result;
}

Matrix operator-(const Matrix& other) const {


Matrix result(rows, cols);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
result.data[i][j] = data[i][j] - other.data[i][j];
}
}
return result;
}
friend std::ostream& operator<<(std::ostream& os, const Matrix& matrix);
};

bool operator==(const Matrix& m1, const Matrix& m2) {


return (m1.rows == m2.rows) && (m1.cols == m2.cols);
}

std::ostream& operator<<(std::ostream& os, const Matrix& matrix) {


for (int i = 0; i < matrix.rows; ++i) {
for (int j = 0; j < matrix.cols; ++j) {
os << matrix.data[i][j] << " ";
}
os << std::endl;
}
return os;
}

int main() {
int rows, cols;

std::cout << "Enter dimensions of matrix 1 (rows and columns): ";


std::cin >> rows >> cols;
Matrix m1(rows, cols);
m1.inputMatrix();

std::cout << "Enter dimensions of matrix 2 (rows and columns): ";


std::cin >> rows >> cols;
Matrix m2(rows, cols);
m2.inputMatrix();

if (m1 == m2) {
Matrix m3 = m1 + m2;
Matrix m4 = m1 - m2;

std::cout << "Matrix 1 + Matrix 2:" << std::endl << m3;


std::cout << "Matrix 1 - Matrix 2:" << std::endl << m4;
} else {
std::cout << "Error: Matrices are not compatible for addition and subtraction." << std::endl;
}

return 0;
● }
5th Program

#include <iostream>
#include <string>

class Father {
protected:
std::string firstName;
std::string surname;
std::string dob;
double bankBalance;

public:
// Constructor for Father class
Father(const std::string& fName, const std::string& sName, const std::string& dateOfBirth,
double balance)
: firstName(fName), surname(sName), dob(dateOfBirth), bankBalance(balance) {}

// Display Father details


void displayFatherDetails() const {
std::cout << "Father's Details:" << std::endl;
std::cout << "First Name: " << firstName << std::endl;
std::cout << "Surname: " << surname << std::endl;
std::cout << "DOB: " << dob << std::endl;
std::cout << "Bank Balance: $" << bankBalance << std::endl;
}
};

class Son : public Father {


private:
// Additional data member specific to Son
std::string sonFirstName;

public:
// Constructor for Son class
Son(const std::string& fName, const std::string& sName, const std::string& dateOfBirth,
double balance, const std::string& sonFName)
: Father(fName, sName, dateOfBirth, balance), sonFirstName(sonFName) {}

// Display Son details, including inherited details from Father


void displaySonDetails() const {
std::cout << "\nSon's Details:" << std::endl;
std::cout << "First Name: " << sonFirstName << std::endl;
std::cout << "Surname: " << surname << std::endl;
std::cout << "DOB: " << dob << std::endl;
std::cout << "Bank Balance: $" << bankBalance << std::endl;
}
};

int main() {
// Create and initialize Father object
Father F1("John", "Doe", "01/01/1970", 100000.0);
F1.displayFatherDetails();

// Create and initialize Son object


Son S1("Jack", "Doe", "15/05/1995", 50000.0, "Junior");
S1.displaySonDetails();

return 0;
}
6th Program

#include <iostream>

class SON; // Forward declaration

class FATHER {
private:
float income;

public:
FATHER(float inc) : income(inc) {}

// Declare friend function


friend float calculateTotalIncome(const FATHER &father, const SON &son);
};

class SON {
private:
float income;

public:
SON(float inc) : income(inc) {}

// Declare friend function


friend float calculateTotalIncome(const FATHER &father, const SON &son);
};

// Friend function definition


float calculateTotalIncome(const FATHER &father, const SON &son) {
return father.income + son.income;
}

int main() {
// Create objects for FATHER and SON
FATHER father(50000.0); // Replace 50000.0 with actual father's income
SON son(25000.0); // Replace 25000.0 with actual son's income

// Calculate and display total family income using friend function


std::cout << "Total Family Income: " << calculateTotalIncome(father, son) << std::endl;

return 0;
}
7th Program

#include <iostream>
#include <string>

class Student;

// Friend function declaration


float mark_avg(const Student &student);

class Student {
private:
std::string name;
float mark1, mark2, mark3;

public:
// Method to get student data
void get_data() {
std::cout << "Enter student name: ";
std::cin >> name;

std::cout << "Enter marks for three subjects:" << std::endl;


std::cout << "Mark 1: ";
std::cin >> mark1;
std::cout << "Mark 2: ";
std::cin >> mark2;
std::cout << "Mark 3: ";
std::cin >> mark3;
}

// Friend function declaration


friend float mark_avg(const Student &student);

// Method to display student name and average marks


void display() {
std::cout << "Student Name: " << name << std::endl;
std::cout << "Average Marks: " << mark_avg(*this) << std::endl;
}
};

// Friend function definition


float mark_avg(const Student &student) {
return (student.mark1 + student.mark2 + student.mark3) / 3.0;
}

int main() {
// Create an object for the Student class
Student student;

// Get student data


student.get_data();

// Display student name and average marks


student.display();

return 0;
}
8th Program

#include <iostream>
#include <string>

class EMPLOYEE {
private:
int employeeNumber;
std::string employeeName;
int basicSalary;
int allAllowances;
int netSalary;

public:
// Member function to read data of an employee
void readData() {
std::cout << "Enter Employee Number: ";
std::cin >> employeeNumber;

std::cout << "Enter Employee Name: ";


std::cin.ignore(); // Ignore any newline characters in the buffer
std::getline(std::cin, employeeName);

std::cout << "Enter Basic Salary: ";


std::cin >> basicSalary;

std::cout << "Enter All Allowances: ";


std::cin >> allAllowances;
}

// Member function to calculate net salary


void calculateNetSalary() {
float incomeTax = 0.3 * (basicSalary + allAllowances);
netSalary = basicSalary + allAllowances - incomeTax;
}

// Member function to print values of data members


void printData() const {
std::cout << "\nEmployee Details\n";
std::cout << "Employee Number: " << employeeNumber << std::endl;
std::cout << "Employee Name: " << employeeName << std::endl;
std::cout << "Basic Salary: " << basicSalary << std::endl;
std::cout << "All Allowances: " << allAllowances << std::endl;
std::cout << "Net Salary: " << netSalary << std::endl;
}
};
int main() {
// Create an object of EMPLOYEE class
EMPLOYEE employee;

// Read data, calculate net salary, and print details


employee.readData();
employee.calculateNetSalary();
employee.printData();

return 0;
}

You might also like