Untitled

 avatar
unknown
plain_text
5 months ago
9.6 kB
11
Indexable
1. Write a program to print the gross salary two employees having basic
salaries of Rs, 21000 and Rs, 35000 respectively. by creating a class named
'Salary' with a function named 'gsalary' which returns the gross salary. HRA
and DA are passed as parameters to its constructor.
Gross Salary = HRA + DA (Assume HRA and DA in Rs. )
CODE:


#include<iostream>
using namespace std;
class Salary{
private:
int HRA;
int DA;
int basic_salaray;
int gross_salary;
public:
Salary ( int b,int h, int d){
basic_salaray = b;
HRA = h;
DA = d;
}
void gsalary(){
gross_salary = HRA + DA+ basic_salaray;
cout<<"The gross salary of employe is
"<<gross_salary<<"Rs"<<endl;
}
};
int main(){
Salary employee1(21000,2000,1000),employee2(35000,3000,5000);
employee1.gsalary();
employee2.gsalary();
}


2. Print the sum and product of two complex numbers by creating a class
named 'Complex' with separate functions for each operation whose real and
imaginary parts are entered by the user.
CODE:


#include<iostream>
using namespace std;
class Complex{
private:
int real;
int imaginary;
public:
void inp(){
cout<<"enter real : ";
cin>>real;
cout<<"enter imaginary : ";
cin>>imaginary;
}
void sum(Complex num1,Complex num2){
real = num1.real + num2.real;
imaginary = num1.imaginary + num2.imaginary;
if (imaginary<0) {
cout<<"the sum of given two numbers is :
"<<real<<imaginary<<"i"<<endl;
}
else{
cout<<"the sum of given two numbers is :
"<<real<<"+"<<imaginary<<"i"<<endl;
}
}
void product(Complex num1,Complex num2){
real = num1.real*num2.real -
num1.imaginary*num2.imaginary;
imaginary = num1.real*num2.imaginary +
num1.imaginary*num2.real;
if (imaginary<0) {
cout<<"the product of given two numbers is :
"<<real<<imaginary<<"i"<<endl;
}else{
cout<<"the product of given two numbers is :
"<<real<<"+"<<imaginary<<"i"<<endl;
}
}
};
int main(){
Complex num1,num2,num3;
num1.inp();
num2.inp();
num3.sum(num1,num2);
num3.product(num1,num2);
}



3. Suppose you have a Piggie Bank with an initial amount of Rs. 500 and
you have to add some more amount to it. Create a class 'AddAmount' with a
data member named 'amount' with an initial value of Rs. 1000. Now make
two constructors of this class as follows:
1 - without any parameter - no amount will be added to the Piggie Bank
2 - having a parameter which is the amount that will be added to the Piggie
Bank
Create an object of the 'AddAmount' class and display the final amount in the
Piggie Bank.
CODE:


#include<iostream>
using namespace std;
class AddAmount{
private:
int amount;
public:
AddAmount(){
amount = 500 ;
}
AddAmount(int piggebank){
amount =500 + piggebank;
}
void disp(){
cout<<"bank "<<amount;}
};
int main(){
AddAmount person1,person2(1000);
person1.disp();
person2.disp();
}



4. Make a class named Cricket with a data member name , age , and to
print number of centuries scored . Create other class named Hockey to print
number of goals scored . Print the total number of matches played by both
players along with their name and age. Use single inheritance.
CODE:



#include<iostream>
#include<string>
using namespace std;
class Cricket{
protected:
string name;
int age;
int num_of_matches_played;
int num_of_centuries;
public:
void input(){
cout<<"name";
cin>>name;
cout<<"age";
cin>>age;
cout<<"matches";
cin>>num_of_matches_played;
}
void inpdata(){
input();
cout<<"centuries";
cin>>num_of_centuries;
}
void display(){
cout<<"name"<<name<<endl;
cout<<"age"<<age<<endl;cout<<"matches"<<num_of_matches_played<<endl;
}
void dispdata(){
display();
cout<<"centuries"<<num_of_centuries<<endl;
}
};
class Hockey : private Cricket{
private:
int num_of_goals;
public:
void inp(){
input();
cout<<"goals";
cin>>num_of_goals;
}
void disp(){
display();
cout<<"goals"<<num_of_goals<<endl;
}
};
int main(){
Cricket angdu;
Hockey bangdu;
angdu.inpdata();
bangdu.inp();
angdu.dispdata();
bangdu.disp();
}



5. Given a pointer to a variable a, the task is to complete the function
updateVar() which will increment the value of the variable by 10. The function
does not returns anything. Also take another variable and increment it by 20.
CODE:



#include<iostream>
using namespace std;
int main(){
int a;int b;
cout<<"a : ";
cin>>a;
cout<<"b : ";
cin>>b;
void updateVar(int*
,int b); // function declare
updateVar(&a,10);//funcion call
updateVar(&b,20);
cout<<"after incrementing a"<<a<<endl;
cout<<"after incrementing b"<<b<<endl;
}
//function defiation
void updateVar(int *num1,int num2){
*num1 += num2;
}
6. Write a C++ program to dynamically allocate an integer, a character and
a string and assign a value to them.
CODE:



#include<iostream>
#include<string>
using namespace std;
int main(){
int *integer = new int;
char *character = new char;
string *str = new string;
cout<<"integer";
cin>>*integer;
cout<<"character";
cin>>*character;
cout<<"str";
cin>>*str;
cout<<"integer"<<*integer<<endl;
cout<<"character"<<*character<<endl;
cout<<"str"<<*str<<endl;delete integer;
delete character;
delete str;
}


7. conversion.
CODE:
Write a C++ program to demonstrate implicit and explicit type


#include <iostream>
using namespace std;
int main() {
int number_of_items;
int totalCost = 0;
double itemPrice;
cout << "Enter the number of items: ";
cin >> number_of_items;
for (int i = 1; i <= number_of_items; i++) {
cout << "Enter the price of item " << i << ": ";
cin >> itemPrice;
totalCost += (int)itemPrice;
}
cout << "Rounded cost : Rs" << totalCost << endl;
return 0;
}




8. Write a C++ program to demonstrate overloading the increment and
decrement operators.
CODE:


#include<iostream>
using namespace std;
class update{
private:
int num;
public:void inp(){
cout<<"num ";
cin>>num;
}
void operator ++ (){
++num;
}
void operator -- (){
--num;
}
void display(){
cout<<"value is "<<num<<endl ;
}
};
int main(){
update num1,num2;
num1.inp();
num2.inp();
++num1;
--num2;
num1.display();
num2.display();
}


9. Write a C++ program to read and print students information using two
classes and simple inheritance.
CODE:



class Student {
protected:
string name;
int rollNumber;
public:
void getStudentInfo() {
cout << "Enter student's name: ";
cin>>name;
cout << "Enter roll number: ";cin >> rollNumber;
}
void displayStudentInfo() {
cout << "Name: " << name << endl;
cout << "Roll Number: " << rollNumber << endl;
}
};
class Marks : public Student {
private:
float marks;
public:
void getMarks() {
cout << "Enter marks: ";
cin >> marks;
}
void displayMarks() {
displayStudentInfo();
cout << "Marks: " << marks << endl;
}
};
int main() {
Marks student1;
student1.getStudentInfo();
student1.getMarks();
cout << "\n--- Student Information ---\n";
student1.displayMarks();
return 0;
}



10. Write a function called safe_divide that takes two double arguments and
returns a double corresponding to the result of dividing the first argument by
the second argument. Before performing the division, the function shouldcheck to determine if division by zero would occur. If division by zero would
occur, instead of performing the division, the function should throw an
exception of type std::invalid_argument.
CODE:



#include <iostream>
#include<string>
using namespace std;
double safe_divide(double num,double den){
if (den != 0){
return num/den;
}
else{
throw(0);
}
}
int main() {
double numerator;
double denominator;
cout << "Enter the numerator: ";
cin>>numerator;
cout << "Enter the denominator: ";
cin>>denominator;
try{
cout<<"dividion is
"<<safe_divide(numerator,denominator)<<endl;
}catch (int num){
cout<<"std::invalid_argument denominator cannot be
"<<num<<endl;
}
return 0;
}



11. Develop a template class Complex that represents a complex number
and is parameterized on the type T used to represent the real and imaginary
parts of the complex number. So, for example, Complex<float> would be a
complex number with the real and imaginary parts represented with floats.
CODE:



#include <iostream>
using namespace std;
template <typename T>
class Complex {
private:
T real;
T imaginary;
public:
void input() {
cout << "Enter real part: ";
cin >> real;
cout << "Enter imaginary part: ";
cin >> imaginary;
}
void display(){
if (imaginary < 0)
cout << real << imaginary << "i" << endl;
else
cout << real << "+" << imaginary << "i" << endl;
}
};
int main() {
Complex<float> num1;
Complex<int> num2;
cout << "Enter first complex number:" << endl;
num1.input();
cout << "Enter second complex number:" << endl;
num2.input();
num1.display();
num2.display();
return 0;
}


12. Write a C++ program to implement a class called Circle that has private
member variables for radius. Include member functions to calculate the
circle's area and circumference.
CODE:



#include <iostream>
using namespace std;
class Circle {
private:
double radius;
public:
void inp() {
cout<<"enter radius ";
cin>>radius;
}
double calculateArea(){
return 3.14 * radius * radius;
}
double calculateCircumference(){
return 2 * 3.14 * radius;
}
void display(){
cout << "Circle with radius: " << radius << endl;
cout << "Area: " << calculateArea() << endl;
cout << "Circumference: " << calculateCircumference() <<
endl;
}
};
int main() {
Circle c;
c.inp();
c.display();
return 0;
}
Editor is loading...
Leave a Comment