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

Sushilcpp

The document contains code for multiple C++ programs written by Sushil Harsana. It includes programs that: 1) Determine if the roots of a quadratic equation are real or imaginary. 2) Calculate electricity bills based on consumption tiers and adds a surcharge if the total is over 300. 3) Defines a BankAccount class with functions to assign values, deposit, withdraw, and display balances. The document contains the student's name, section, and roll number before each code sample. It provides 20 different programming challenges and their solutions.

Uploaded by

Sushil Gurjar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Sushilcpp

The document contains code for multiple C++ programs written by Sushil Harsana. It includes programs that: 1) Determine if the roots of a quadratic equation are real or imaginary. 2) Calculate electricity bills based on consumption tiers and adds a surcharge if the total is over 300. 3) Defines a BankAccount class with functions to assign values, deposit, withdraw, and display balances. The document contains the student's name, section, and roll number before each code sample. It provides 20 different programming challenges and their solutions.

Uploaded by

Sushil Gurjar
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 66

Name - Sushil Harsana

Section - F
Roll no - 53
/* Question 1. Given the roots of quadratic polynomial(float variables). WAP to
determine whether roots are real or imaginary. If roots are real find them otherwise write
message "No real roots".*/

#include <iostream>

#include <math.h>

using namespace std;

int main(int argc, char const *argv[])

int r1,r2,d,a,b,c;

cout<<"Enter the value of a,b,c : ";

cin>>a>>b>>c;

d=sqrt((pow(b,2))-(4*a*c));

if (d==0)

r1=r2=(-b/2*a);

cout<<"Roots are : "<<r1<<" "<<r2;

else if (d>0)

r1=(-b+d)/(2*a);

r2=(-b-d)/(2*a);

cout<<"Roots are : "<<r1<<" "<<r2;

else

cout<<"NO real roots";

    return 0;}
/* Question 3. An electricity board charges the following domestic user to discourage large
consumption of electricity :
For first 100 units -- 0.60/unit
For next 200 units -- 0.80unit
For more than 300 unit -- 0.90/unit
All users a charged a minimum of 50 rupees. If total amount is greater than 300 than a
subcharge of 15% is added. Write a program to read names of users and number of units
consumed and display charge with name.*/

#include <iostream>
#include <string>
using namespace std;
struct bill
{
string name;
int units;
float cost;
};
int main(int c, char v[])
{
int n;
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
cout<<"Enter the number of candidates : ";
cin>>n;
bill b1[n];
for (int i = 0; i < n; i++)
{
cout<<"Enter "<<i+1<<" name : ";
cin.ignore();
getline(cin,b1[i].name);
cout<<"Enter your units : ";
cin>>b1[i].units;
}
for (int i = 0; i < n; i++)
{
if (b1[i].units <= 100)
b1[i].cost = b1[i].units * 0.6;
else if(b1[i].units >= 100 && b1[i].units <= 300)
{
b1[i].cost=0.8 * b1[i].units;
b1[i].cost = b1[i].cost + 50;
}
else if(b1[i].units>300)
{
b1[i].cost = b1[i].units * 0.9;
b1[i].cost = b1[i].cost + 50;
}
if (b1[i].cost>=300)
{
b1[i].cost = b1[i].cost + (b1[i].cost*0.15);
}
cout<<"Name of coutomer "<<i+1<<" : "<<b1[i].name<<endl;
cout<<"charges : "<<b1[i].cost<<endl;
}
return 0;
}
/*Question 5. Write a program in C++ ,defining a class to represent bank account, which
includes :
Data Members :

 Name of depositer
 Type of account(Savings or Current)
 Account number

Balance amount in account
Member function:

 To assign initial value


 To deposit an amount
 To withdraw an amount
 To display name and balance*/

#include <iostream>
#include <string>
using namespace std;
class bankacount
{
string name;
int account_no;
string type_of_account;
int balance_amount;
public:
void getinfo()
{
cout<<"Type of account saving/current : ";
getline(cin,type_of_account);
cout<<"Enter your name : ";
getline(cin,name);
cout<<"Enter your account number : ";
cin>>account_no;
cout<<"Enter the balance amount : ";
cin>>balance_amount;
}
void deposite()
{
int n;
cout<<"Enter the amount you wants to deposite : ";
cin>>n;
balance_amount=balance_amount+n;
}
void withdraw()
{
int n;
cout<<"Your balance is : "<<balance_amount<<endl;
cout<<"Enter the amount you wants to withdraw : ";
cin>>n;
balance_amount=balance_amount-n;
}
void display()
{
cout<<"Name : "<<name<<endl;
cout<<"Balance : "<<balance_amount<<endl;
}
};
int main()
{
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
bankacount ob;
ob.getinfo();
ob.deposite();
ob.withdraw();
ob.display();
return 0;
}
/* Question 19 Templates are the foundation of generic programming, which involves writing
code in a way that is independent of any particular type. Write a program that can create a list
(create a class list) of given type (int, float, char etc.) and perform insertion and
deletion on list object.*/
#include <iostream>
#include <vector>
template <typename T>
class List {
private:
std::vector<T> data;

public:
void insert(T element) {
data.push_back(element);
}

void deleteElement(T element) {


for (int i = 0; i < data.size(); i++) {
if (data[i] == element) {
data.erase(data.begin() + i);
return;
}
}
}

void display() {
for (int i = 0; i < data.size(); i++) {
std::cout << data[i] << " ";
}
std::cout << "\n";
}
};
int main() {
std::cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167";
List<int> intList;
intList.insert(1);
intList.insert(2);
intList.insert(3);
std::cout << "Int list: ";
intList.display();

List<float> floatList;
floatList.insert(1.1);
floatList.insert(2.2);
floatList.insert(3.3);
std::cout << "Float list: ";
floatList.display();

List<char> charList;
charList.insert('a');
charList.insert('b');
charList.insert('c');
std::cout << "Char list: ";
charList.display();
return 0;
}
/* Question 6. Create a class called Invoice that a hardware store might use to represent an
invoice for an item sold at the store. An Invoice should include four pieces of information
as instance

Data Members:

 partNumber (type String)


 partDescription (type String)
 quantity of the item being purchased (type int )
 price_per_item (type double)

Your class should have a constructor that initializes the four instance variables. Provide a
set and a get method for each instance variable. In addition, provide a method named
getInvoiceAmount() that calculates the invoice amount (i.e., multiplies the quantity by
the price per item), then returns the amount as a double value. If the quantity is not
positive, it should be set to 0. If the price per item is not positive, it should be set to

0.0. Write a test application named invoiceTest that demonstrates class Invoice’s
capabilities. Class, Objects, Data members, Member Functions and Constructor.
*/
#include <iostream>
#include <string>
using namespace std;
class invoice
{
string partno;
string partdes;
int quantity;
int price_per_item;
public:
invoice()
{
partno="0";
partdes="0";
quantity=0;
price_per_item=0;
}
void get_pn()
{
cout<<"Enter part number : ";
getline(cin,partno);
}
void get_pd()
{
cout<<"Enter part description : ";
getline(cin,partdes);
}
void get_q()
{
cout<<"Enter quantity : ";
cin>>quantity;
}
void get_ppi()
{
cout<<"Enter price of the item : ";
cin>>price_per_item;
}
void set_q()
{
if(quantity<0)
quantity=0;
}
void set_ppi()
{
if(price_per_item<0)
price_per_item=0;
}
double getinvoiceamount()
{
double ia;
ia=price_per_item*quantity;
return ia;
}
void invoicetest(double a)
{
cout<<"Part number = "<<partno<<endl<<"Part description =
"<<partdes<<endl<<"Total amount = "<<a<<endl;
}
};
int main()
{
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
double amount;
invoice ob;
ob.get_pn();
ob.get_pd();
ob.get_q();
ob.get_ppi();
ob.set_q();
ob.set_ppi();
amount=ob.getinvoiceamount();
ob.invoicetest(amount);
return 0; }
/*Question 7. Imagine a tollbooth with a class called TollBooth. The two data items are of
type unsigned int and double to hold the total number of cars and total amount of money
collected. A constructor initializes both of these data members to
0. A member function called payingCar( ) increments the car total and adds 0.5 to the
cash total. Another function called nonPayCar( ) increments the car total but adds
nothing to the cash total. Finally a member function called display( ) shows the two
totals. Include a program to test this class. This program should allow the user to push
one key to count a paying car , and another to count a non paying car. Pushing the ESC
key should cause the program to print out the total number of cars and total cash and
then exit.*/

#include <iostream>
using namespace std;
class Tollbooth
{
int cars;
double amount;
public:
Tollbooth()
{
cars=0;
amount=0;
}
void payingcars()
{
cars++;
amount=amount+0.5;
}
void nonpayingcars()
{
cars++;
}
void display()
{
cout<<"Total cars = "<<cars<<endl;
cout<<"Total amount = "<<amount<<endl;
}
};
int main()
{
Tollbooth ob;
char ch;
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
while (1)
{
cout<<"Press 1 for payingcars\nPress 2 for nonpayingcars\nPress E to exit"<<endl;
cin>>ch;
switch (ch)
{
case '1':
ob.payingcars();
break;
case '2':
ob.nonpayingcars();
break;
case 'E':
break;
default:
cout<<"Wrong input";
break;
}
if (ch=='E')
break;
}
ob.display();
return 0;
}
/*Question 8. Create a class called Time that has separate int member data for hours,
minutes and seconds. One constructor should initialize this data to 0, and another should
initialize it to fixed values. A member function should display it in 11:59:59 format. A
member function named add() should add two objects of type time passed as arguments.
A main ( ) program should create two initialized values together, leaving the result in the
third time variable. Finally it should display the value of this third variable.*/

#include <iostream>
#include <string>
using namespace std;
class Time
{
int hours,minutes,seconds;
public:
Time()
{
hours=0;
minutes=0;
seconds=0;
}
Time(int a,int b,int c)
{
hours=a;
minutes=b;
seconds=c;
}
void display()
{
cout<<hours<<":"<<minutes<<":"<<seconds<<endl;
}
void add(Time x,Time y)
{
seconds=x.seconds+y.seconds;
if (seconds>59)
{
seconds-=60;
minutes++;
}
minutes+=x.minutes+y.minutes;
if (minutes>59)
{
minutes-=60;
hours++;
}
hours+=x.hours+y.hours;
}
};
int main()
{
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
Time a(1,46,50),b(4,05,50);
Time c;
c.add(a,b);
c.display();
return 0;
}
/*Question 2. Enter a 5*3 matrix and search for even and odd numbers row wise and
column wise.*/

#include <iostream>
using namespace std;
int main()
{
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
int a[5][3],i,j;
cout<<"Enter elements : "<<endl ; for(i=0; i<5; i++)
for(j=0; j<3; j++) cin>>a[i][j];
for(i=0; i<5; i++)
{

cout<<"\nEven Numbers in row "<<i+1<<" : "<<endl; for(j=0; j<3; j++)


{

if(a[i][j]%2==0)
cout<<a[i][j]<<" ";
}

cout<<"\nOdd Numbers in row "<<i+1<<" : "<<endl; for(j=0; j<3; j++)


{

if(a[i][j]%2!=0)
cout<<a[i][j]<<" ";
}
}

for(j=0; j<3; j++)


{
cout<<"\nEven Numbers in column "<<j+1<<" : "<<endl; for(i=0; i<5; i++)
{

if(a[i][j]%2==0)
cout<<a[i][j]<<" ";
}

cout<<"\nOdd Numbers in column "<<j+1<<" : "<<endl; for(i=0; i<5; i++)


{

if(a[i][j]%2!=0)
cout<<a[i][j]<<" ";
}
}

return 0;
}
/*Question 18 Implement a C++ program to demonstrate and understand Diamond problem*/
#include<iostream>
using namespace std;
class Person {
// Data members of person
public:
Person(int x) { cout << "Person::Person(int ) called" << endl; }
};

class Faculty : public Person {


// data members of Faculty
public:
Faculty(int x):Person(x) {
cout<<"Faculty::Faculty(int ) called"<< endl;
}
};

class Student : public Person {


// data members of Student
public:
Student(int x):Person(x) {
cout<<"Student::Student(int ) called"<< endl;
}
};

class TA : public Faculty, public Student {


public:
TA(int x):Student(x), Faculty(x) {
cout<<"TA::TA(int ) called"<< endl;
}
};

int main() {
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
TA ta1(30);
}
Question 9. Using the concept of operator overloading.Write a program to overload using
with and without friend Function.

a. Unary –

b. Unary ++ preincrement, postincrement

c. Unary -- predecrement, postdecrement*/

#include<iostream>
using namespace std; class Test
{

private:
int a; public:
void setData()
{

cout<<"Enter number : "; cin>>a;


}

Test operator-()
{

Test temp; temp.a=-a; return temp;

}
Test operator--()
{

Test temp; temp.a=--a; return temp;


}

Test operator--(int)
{

Test temp; temp.a=a--; return temp;


}

Test operator++()
{

Test temp; temp.a=++a; return temp;


}

Test operator++(int)
{

Test temp; temp.a=a++;

return temp;
}

void show()
{

cout<<a<<endl;
}
};

int main()
{
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
Test ob,ob1; ob.setData(); ob1=-ob;
cout<<"Before Unary - : "; ob.show();
cout<<"After Unary - : "; ob1.show();
cout<<endl;
cout<<"Before Predecrement : "; ob.show();
ob1=--ob;
cout<<"After Predecrement : "; ob1.show();
cout<<endl;

cout<<"Before Postdecrement : "; ob.show();


ob1=ob--;
cout<<"After Postdecrement : "; ob1.show();
cout<<endl;
cout<<"Before Preincrement : "; ob.show();
ob1=++ob;
cout<<"After Preincrement : "; ob1.show();
cout<<endl;
cout<<"Before Postincrement : "; ob.show();
ob1=ob++;
cout<<"After Postincrement : "; ob1.show();
return 0;
}
/* Question 10. Create a class Complex having two int type variable named real & img
denoting real and imaginary part respectively of a complex number. Overload
+ , - , == operator to add, to subtract and to compare two complex numbers being denoted
by the two complex type objects.*/
#include <iostream>
using namespace std; class complex1{
private:
int real,img; public:
void setData(){ cout<<"Enter real part : "; cin>>real;
cout<<"Enter imaginary part : "; cin>>img;
}
complex1 operator+(complex1 &o){ complex1 temp; temp.real=real+o.real;
temp.img=img+o.img;
return temp;

}
complex1 operator-(complex1 &o){ complex1 temp;
if(real>o.real)
{

temp.real=real-o.real; temp.img=img-o.img;
}

else
{

temp.real=o.real-real; temp.img=o.img-img;
}

return temp;
}
int operator==(complex1 &o)
{

if(real==o.real && img==o.img) return 1;


else
return 0;;
}

void show()
{

if(img>=0)
cout<<real<<" +i"<<img<<endl; else
cout<<real<<" -i"<<abs(img)<<endl;
}
};

int main()
{
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
complex1 ob1,ob2,ob3; ob1.setData(); ob2.setData(); ob3=ob1+ob2; cout<<"Addition : ";
ob3.show();
ob3=ob1-ob2;
cout<<"Subtraction";
ob3.show();
if(ob1==ob2)
cout<<"Equal"<<endl;
else
cout<<"Not equal"<<endl;
return 0;
}
/*Question 11. Write A program in C++ to create a class employee having attributes name,
department, id, salary, where components of salary are as follows :

Basic Salary, HRA, DA, TA, MISC

where HRA = 15% of basic salary

DA = 10% of basic

salary TA = 5% of

basic salary

Considering the organization having n employees. Find total salary of each employee
and search for an employee using id.*/

#include<iostream>
#include<string>
using namespace std;
class employee
{

string name,department; int id,basic;


float salary,hra,da,ta,misc; public:
void setData()
{

cout<<"Enter name : "; cin.ignore();


getline(cin,name);
cout<<"Enter department : "; getline(cin,department); cout<<"Enter id : ";
cin>>id;
cout<<"Enter basic salary : "; cin>>basic;
cout<<"Enter misc : "; cin>>misc; hra=basic*0.15; da=basic*0.1; ta=basic*0.05;
salary=basic+hra+da+ta+misc;
}

void show()
{

cout<<"Name : "<<name<<endl; cout<<"Department : "<<department<<endl; cout<<"ID :


"<<id<<endl;
cout<<"Salary : "<<salary<<endl;
}

int getID()
{

return id ;

}
};

int main()
{
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
int n,i,id;
cout<<"Enter no. of employees : "; cin>>n;
employee ob[n]; for(i=0; i<n; i++)
ob[i].setData(); for(i=0; i<n; i++)
ob[i].show();
cout<<"Enter employee id to search : "; cin>>id;
for(i=0; i<n; i++) if(ob[i].getID()==id)
ob[i].show();

return 0;
}
/*Question 12. We are given a list of n-1 integers and these are in the range of 1 to n.
There are no duplicates in the list. One of the integer is missing in the list. Write a C++
program to find the missing integer.*/

#include<iostream>
using namespace std;
int main()
{
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
int n,i,c=0,j; cout<<"Enter n : ";
cin>>n;
int a[n-1];
cout<<"Enter numbers : "<<endl; for(i=0; i<n-1; i++)
cin>>a[i]; for(i=1; i<=n; i++)
{

for(j=0; j<n-1; j++)


{

if(i==a[j])
{
c=1;
break;

}
}

if(c!=1)
{

cout<<"Missing Number is "<<i<<endl; break;


}

c=0;
}
return 0;
}
/*Question 16 Create a Base class that consists of private, protected and public data members
and member functions.
Try using different access modifiers for inheriting Base class to the Derived class and create a
table that summarizes the above three modes (when derived in public, protected and private
modes) and shows the access specifier of the members of base class in the Derived class.
Inheritance, Access Specifiers*/

#include <iostream>
using namespace std;

class Base {
private:
int privateData;

protected:
int protectedData;

public:
int publicData;

void privateFunction() {
cout<<" private data" <<endl;
// privateFunction can only be accessed from within the Base class
}

void protectedFunction() {
cout<<" protected data" <<endl;
// protectedFunction can be accessed from within the Base class and from derived
classes
}

void publicFunction() {
cout<<" protected data" <<endl;
// publicFunction can be accessed from anywhere
}
};

class DerivedPublic: public Base {


public:
void test() {
// The derived class can access the protected members of the base class
protectedData = 10;
protectedFunction();

// The derived class can also access the public members of the base class
publicData = 20;
publicFunction();
}
};

class DerivedProtected: protected Base {


public:
void test() {
// The derived class can access the protected members of the base class
protectedData = 10;
protectedFunction();

// The derived class can also access the public members of the base class, which are now
protected members in the derived class
publicData = 20;
publicFunction();
}
};
class DerivedPrivate: private Base {
public:
void test() {
// The derived class can access the protected members of the base class, which are now
private members in the derived class
protectedData = 10;
protectedFunction();

// The derived class can also access the public members of the base class, which are now
private members in the derived class
publicData = 20;
publicFunction();
}
};

int main() {
DerivedPublic dp;
DerivedProtected dpr;
DerivedPrivate dprv;

// The main function can access the public members of the derived class inherited in public
mode
dp.publicData = 30;
dp.publicFunction();

// The main function cannot access the protected members of the derived class inherited in
protected mode
// dpr.protectedData = 40;
// dpr.protectedFunction();

// The main function cannot access the private members of the derived class inherited in
private mode
// dprv.privateData = 50;
// dprv.privateFunction();

return 0;
}
/*Question 15 Create a class called Student that contains the data members like age, name,
enroll_no, marks. Create another class called Faculty that contains data members like
facultyName, facultyCode, salary, deptt, age, experience, gender. Create the function
display() in both the classes to display the respective information. The derived Class Person
demonstrates multiple inheritance. The program should be able to call both the base classes
and displays their information. Remove the ambiguity (When we have exactly same variables
or same methods in both the base classes, which one will be called?) by proper mechanism.*/

#include <iostream>
// Base class Student
class Student {
private:
int age;
std::string name;
int enroll_no;
int marks;

public:
Student(int age, std::string name, int enroll_no, int marks)
: age(age), name(name), enroll_no(enroll_no), marks(marks) {}

void display() {
std::cout << "Student Information:\n";
std::cout << "Age: " << age << "\n";
std::cout << "Name: " << name << "\n";
std::cout << "Enroll No.: " << enroll_no << "\n";
std::cout << "Marks: " << marks << "\n";
}
};

// Base class Faculty


class Faculty {
private:
std::string facultyName;
int facultyCode;
int salary;
std::string deptt;
int age;
int experience;
char gender;

public:
Faculty(std::string facultyName, int facultyCode, int salary, std::string deptt, int age, int
experience, char gender)
: facultyName(facultyName), facultyCode(facultyCode), salary(salary), deptt(deptt),
age(age), experience(experience), gender(gender) {}

void display() {
std::cout << "Faculty Information:\n";
std::cout << "Faculty Name: " << facultyName << "\n";
std::cout << "Faculty Code: " << facultyCode << "\n";
std::cout << "Salary: " << salary << "\n";
std::cout << "Department: " << deptt << "\n";
std::cout << "Age: " << age << "\n";
std::cout << "Experience: " << experience << "\n";
std::cout << "Gender: " << gender << "\n";
}
};

// Derived class Person demonstrating multiple inheritance


class Person: public Student, public Faculty {
public:
Person(int age, std::string name, int enroll_no, int marks,
std::string facultyName, int facultyCode, int salary, std::string deptt, int experience,
char gender)
: Student(age, name, enroll_no, marks), Faculty(facultyName, facultyCode, salary, deptt,
age, experience, gender) {}

// Overriding the display() function to resolve ambiguity


void display() {
// Call the display() function of the Student base class
Student::display();

// Call the display() function of the Faculty base class


Faculty::display();
}
};

int main() {
std::cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167";
Person p(21, "John", 12345, 85, "Jane", 54321, 70000, "Computer Science", 5, 'F');

// Call the display() function of the derived class Person


p.display();

return 0;
}
/*Question 20 C++ program to implement different methods of List, Vector and Map in STL
(Standard Template Library)*/
#include<iostream>
#include<list>
#include<vector>
#include<map>

using namespace std;

int main()
{
vector<int> demo1={1,2,3,4,5};
list<float> demo2{6.6,7.7,8.8,9.9,10.0};
map<int,string> demo3{ {1,"DAMAN"},{2,"RAKWAL"} };

cout<<"Displaying vector:"<<endl;
for(int i:demo1)
{
cout<<i<<endl;
}

cout<<"Displaying list:"<<endl;
for(float i:demo2)
{
cout<<i<<endl;
}

cout<<"Displaying map:"<<endl;
for(auto i:demo3)
{
cout<<i.first<<":"<<i.second<<endl;
}

return 0;
}
/*Question13 Implement a c++ program to create a class called ConsDemo and overload
SumDemo () function. SumDemo (int, char)→If passing character is ‘p’ then print square of
passing number otherwise cube of a number SumDemo (int, int, char)→If passing character
is ‘a’ then print addition of numbers otherwise print Ascii value of a passing character.
SumDemo (string, string)->Check whether passing strings are equal or not.*/
#include <iostream>
#include <string>
class ConsDemo
{
public:
// Overloaded SumDemo function with one int and one char argument
void SumDemo(int x, char c)
{
if (c == 'p')
std::cout << "Square of " << x << ": " << x * x << std::endl;
else
std::cout << "Cube of " << x << ": " << x * x * x << std::endl;
}

// Overloaded SumDemo function with two int and one char argument
void SumDemo(int x, int y, char c)
{
if (c == 'a')
std::cout << "Sum of " << x << " and " << y << ": " << x + y << std::endl;
else
std::cout << "ASCII value of " << c << ": " << static_cast<int>(c) << std::endl;
}

// Overloaded SumDemo function with two string arguments


void SumDemo(std::string s1, std::string s2)
{
if (s1 == s2)
std::cout << "Strings are equal" << std::endl;
else
std::cout << "Strings are not equal" << std::endl;
}
};

int main()
{
std::cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167";
ConsDemo obj;

// Call SumDemo with one int and one char argument


obj.SumDemo(5, 'p');

// Call SumDemo with two int and one char argument


obj.SumDemo(5, 10, 'a');

// Call SumDemo with two string arguments


obj.SumDemo("Hello", "Hello");

return 0;
}
/*Question 17 Create class SavingsAccount. Use a static variable annualInterestRate to store
the annual interest rate for all account holders. Each object of the class contains a private
instance variable savingsBalance indicating the amount the saver currently has on deposit.
Provide method calculateMonthlyInterest to calculate the monthly interest by multiplying the
savingsBalance by annualInterestRate divided by 12.This interest should be added to
savingsBalance. Provide a static method modifyInterestRate that sets the annualInterestRate
to a new value. Write a program to test class SavingsAccount. Instantiate two
savingsAccount objects, saver1 and saver2, with balances of
$2000.00 and $3000.00, respectively. Set annualInterestRate to 4%, then calculate the
monthly interest and print the new balances for both savers. Then set the annualInterestRate
to 5%, calculate the next month’s interest and print the new balances for both savers.static
variable and static method*/
#include <iostream>
class SavingsAccount {
public:
static double annualInterestRate;

SavingsAccount(double savingsBalance) : savingsBalance(savingsBalance) {}

void calculateMonthlyInterest() {
double interest = savingsBalance * annualInterestRate / 12;
savingsBalance += interest;
}

static void modifyInterestRate(double newRate) {


annualInterestRate = newRate;
}

double getSavingsBalance() const {


return savingsBalance;
}

private:
double savingsBalance;
};

double SavingsAccount::annualInterestRate = 0.04;

int main() {
std::cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167\n";
// Create two savings account objects
SavingsAccount saver1(2000.00);
SavingsAccount saver2(3000.00);

// Calculate monthly interest and print new balances


saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
std::cout << "New balances:" << std::endl;
std::cout << "Saver 1: $" << saver1.getSavingsBalance() << std::endl;
std::cout << "Saver 2: $" << saver2.getSavingsBalance() << std::endl;

// Modify the interest rate and calculate next month's interest


SavingsAccount::modifyInterestRate(0.05);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
std::cout << "New balances:" << std::endl;
std::cout << "Saver 1: $" << saver1.getSavingsBalance() << std::endl;
std::cout << "Saver 2: $" << saver2.getSavingsBalance() << std::endl;

return 0;
}
/*Question 19 W.A.P in C++ to show the working of function overloading by using a
function named calculateArea() to calculate area of square, rectangle and triangle using
different signatures as required.*/
#include <iostream>
#include <string>
using namespace std;
float calarea(float s)
{
return (s*s);
}
float calarea(float l,float b)
{
return (l*b);
}
float calarea(float b,float h,float hf)
{
return (hf*b*h);
}
int main()
{
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
float s,l,b,h,half=0.5,area;
int ch;
do
{
cout<<"Press 1 for Area of square\nPress 2 for Area of rectangle\nPress 3 for Area of
triangle\nPress 4 for Exit\n";
cin>>ch;
switch (ch)
{
case 1:
cout<<"Enter side of the square : ";
cin>>s;
area=calarea(s);
cout<<"Area of square : "<<area<<endl;
break;
case 2:
cout<<"Enter the length and breath of the rectangle : ";
cin>>l>>b;
area=calarea(l,b);
cout<<"Area of rectangle : "<<area<<endl;
break;
case 3:
cout<<"Enter base and height of the triangle : ";
cin>>b>>h;
area=calarea(b,h,half);
cout<<"Area of triangle : "<<area<<endl;
break;
case 4:
cout<<"EXIT";
break;
default:
cout<<"Wrong choice";
break;
}
} while (ch!=4);
return 0;}
/*Question 20 Write a Program in C++ to demosntrate the concept of data abstraction using
the concept of Class and Objects*/
#include <iostream>
using namespace std;
class abstraction
{
int a,b;
public:
void set(int x,int y)
{
a=x,
b=y;
}
void display()
{
cout<<"a = "<<a<<endl;
cout<<"b = "<<b<<endl;
}
};
int main()
{
cout<<"Name:Sushil Harsana\nSection:F\nRoll num:2019167"<<endl;
abstraction ob;
ob.set(40,50);
ob.display();
return 0;}
/*Question 14 : Define a class named UserOne with following description:
Implement a C++program to compare Father Name, Mother Name of both classes using
friend function. If Father name and Mother Name of both classes are equal then print the
message “Belongs to Same Family” and display message “ We are Brothers” or “We are
Sisters” or “We are brother and sister”, otherwise print the message “Belongs to different
Family”.
*/

#include<iostream>
using namespace std;
class UserTwo;
class UserOne {
string name;
string fathername;
string mothername;
string gender;
public:
void InputInfo() {
cout<<"Enter name : ";
getline(cin,name);
cout<<"Enter father's name : ";
getline(cin,fathername);
cout<<"Enter mother's name : ";
getline(cin,mothername);
cout<<"Enter gender : ";
cin>>gender;
}
friend void Userchecker(UserOne &,UserTwo &);
};
class UserTwo {
string name;
string fathername;
string mothername;
string gender;
public:
void InputInfo() {
cout<<"Enter name : ";
cin.ignore();
getline(cin,name);
cout<<"Enter father's name : ";
getline(cin,fathername);
cout<<"Enter mother's name : ";
getline(cin,mothername);
cout<<"Enter gender : ";
cin>>gender;
}
friend void Userchecker(UserOne &,UserTwo &);
};
void Userchecker(UserOne &u1,UserTwo &u2) {
if(u1.fathername==u2.fathername&&u1.mothername==u2.mothername) {
cout<<"Belongs to Same Family"<<endl;
if(u1.gender==u2.gender&&u1.gender=="male")
cout<<"We are brothers"<<endl;
else if(u1.gender==u2.gender&&u1.gender=="female")
cout<<"We are sisters"<<endl;
else
cout<<"We are brother and sister"<<endl;
}
else
cout<<"Belongs to different family"<<endl;
}
int main () {
UserOne U1;
UserTwo U2;
U1.InputInfo();
U2.InputInfo();
Userchecker(U1,U2);
}

You might also like