SlideShare uma empresa Scribd logo
1 de 11
Assignment #1
Q#1: Create a class that imitates part of the functionality of the basic data type int. Call the
class Int (note different capitalization). The only data in this class is an int variable. Include
member functions to initialize an Int to 0, to initialize it to an int value, to display it (it looks just
like an int), and to add two Int values. Write a program that exercises this class by creating one
uninitialized and two initialized Int values, adding the two initialized values and placing the
response in the uninitialized value, and then displaying this result.
Program:
#include <iostream>
usingnamespace std;
classInteger
{
private:
int num,num1;
public:
Integer()
{}
Integer(intn)
{
num=n;
}
int add(IntegerInt1,IntegerInt2)
{
num1=Int1.num+Int2.num;
returnnum1;
}
};
intmain()
{
int number;
IntegerInt1(10),Int2(13),Int3;
number=Int3.add(Int1,Int2);
cout<<"1st
number:"<<10<<"n2nd number:"<<13<<endl;
cout<<"n3rd numberissum of 1st and2nd numbers:"<<number<<"n";
}
Output:
1st
number:10
2nd
number:13
3rd
numberissumof 1st and 2nd numbers: 23
Q#2: Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent
toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of
the number of cars that have gone by, and of the total amount of money collected. Model this
tollbooth with a class called tollBooth. The two data items are a type unsigned int to hold the
total number of cars, and a type double to hold the total amount of money collected. A
constructor initializes both of these to 0. A member function called payingCar() increments the
car total and adds 0.50 to the cash total. Another function, called nopayCar(), increments the
car total but adds nothing to the cash total. Finally, a member function called display() displays
the two totals. Make appropriate member functions.
Program:
#include <iostream>
usingnamespace std;
classtollboth
{
private:
int null,pcar,ncar;
floattax;
public:
tollboth()
{
tax=0;
pcar=0;
ncar=0;
null=0;
}
voidpaycar(inta)
{
pcar=pcar+a;
for(null;null<=pcar;null++)
{
tax=tax+0.50;
}
}
voidnopaycar(intb)
{
ncar=ncar+b;
}(
voiddisplay()
{
cout<<"Total no of payedcars are : "<<pcar<<endl;
cout<<"Total tax is : "<<tax<<endl;
cout<<"Total no of not payedcars are : "<<ncar<<endl;
}
};
intmain()
{
tollbothtb;
char press,input;
int a,b;
do{
cout<<"Press1 forcar pay tax"<<endl;
cout<<"Press2 forcar not pay tax"<<endl;
cout<<"Press3 fortotal tax and Exit"<<endl;
cin>>press;
switch(press)
{
case '1':
{
cout<<"EnterNo of the cars pay tax"<<endl;
cin>>a;
tb.paycar(a);
break;
}
case '2':
{
cout<<"EnterNo of cars notpay tax"<<endl;
cin>>b;
tb.nopaycar(b);
break;
}
}
cout<<"Pressy to continue andn forterminate"<<endl;
cin>>input;
}
while(input=='y');
tb.display();
system("pause");
return0;
}
Output:
Press 1 for car pay tax
Press2 forcar not paytax
Press3 fortotal tax and exit
1
Enter Noof the cars pay tax
2
Pressy to continue andnfor terminate
y
Press 1 for car pay tax
Press2 forcar not paytax
Press3 fortotal tax and exit
2
Enter Noof cars not pay tax
2
Pressy to continue andnfor terminate
y
Press 1 for car pay tax
Press2 forcar not paytax
Press3 fortotal tax and exit
3
Pressy to continue andnfor terminate
Y
Total noof payedcars are : 2
Total tax is : 1.5
Total noof notpayedcars are : 2
Q#3: 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. Another member function should display it, in 11:59:59 format. The final member
function should add two objects of type time passed as arguments. A main() program should
create two initialized time objects (should they be const?) and one that isn’t initialized. Then it
should add the two initialized values together, leaving the result in the third time variable.
Finally it should display the value of this third variable. Make appropriate member functions
const.
Program:
#include <iostream>
#include <conio.h>
usingnamespace std;
classtime{
private:
inthours,minutes,seconds;
public:
time(){
hours= minutes=seconds= 0;
}
time(inth,intm,int s){
hours= h;
minutes=m;
seconds= s;
}
voidshowTime() const{
cout << hours<< ':' << minutes<<':' << seconds;
}
voidaddTime(timex,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;
}
};
intmain(){
const time a(2,23,45), b(4,25,15);
time c;
c.addTime(a,b);
c.showTime();
}
Output:
06:49:00
Q#4: Create an employee class. The member data should comprise an int for storing the
employee number and a float for storing the employee’s compensation. Member functions
should allow the user to enter this data and display it. Write a main() that allows the user to
enter data for three employees and display it.
Program:
#include <iostream>
#include <conio.h>
usingnamespace std;
classemployee{
private:
intemp_num;
floatemp_comp;
public:
voidentData(){
cout << "EnterEmployee'sNumber: ";
cin >> emp_num;
cout << "EnterEmployee'sSalary :" ;
cin >> emp_comp;
}
voiddisplay(){
cout << "Employee'sNumber:" << emp_num<< endl;
cout << "Enployee'sSalary: "<< emp_comp<< endl;
}
};
intmain(){
employee emp1,emp2,emp3;
cout << "Enter Data For Employee 1"<< endl;
emp1.entData();
cout << "Enter Data For Employee 2"<< endl;
emp2.entData();
cout << "Enter Data For Employee 3"<< endl;
emp3.entData();
cout << "Total Data EnteredIs : " << endl;
emp1.display();
emp2.display();
emp3.display();
}
Output:
Enter Data For Employee 1
Enter Employee’sNumber:5
Enter Employee’sSalary:20000
Enter Data For Employee 2
Enter Employee’sNumber:6
Enter Employee’sSalary:65000
Enter Data For Employee 3
Enter Employee’sNumber:7
Enter Employee’sSalary:15000
Total Data EnteredIs:
Employee’sNumber 5
Employee’sSalary 20000
Employee’sNumber 6
Employee’sSalary 65000
Employee’sNumber 7
Employee’sSalary 15000
Q#5: Define a class that will hold the set of integers from 0 to 31. An element can be set with
the set member function and cleared with the clear member function. It is not an error to set
an element that's already set or clear an element that's already clear. The function test is used
to tell whether an element is set.
Program:
#include <iostream>
#include<windows.h>
#include<conio.h>
usingnamespace std;
classInteger{
public:
intnumber_array[34];
voidset(intloc,intnum){
number_array[loc]=num;
}
voidclear(){
for(inti=0;i<=31;i++){
number_array[i] =0;
}
}
voidsetArray(){
for(intk=0;k<=31;k++)
{
number_array[k] =k;
}
cout<<"nnCongratulations....nnyouhave succesfully StoredVALUESinARRAY..!";
getch();
}
voidtest(){
intcount=0;
for(intj=0;j<=31;j++)
{
if(number_array[j]==0){
count++;
}
}
if(count>20){
cout<<"array is empty";
}
else
{
for(intj=0;j<=31;j++){
cout<<number_array[j]<<",";
}
}
getch();
}
};
intmain() {
// yourcode goeshere
intnumb,num;
Integera;
do {
system("cls");
cout<<"nnntMAIN MENU";
cout<<"nnt01.Set Array1 to 31 values";
cout<<"nnt02.Test Array";
cout<<"nnt03.ClearArray";
cout<<"nnt04.EXIT";
cout<<"nntSelectYourOption(1-4) ";
cin>>num;
switch(num)
{
case 1:
a.setArray();
break;
case 2:
a.test();
break;
case 3:
a.clear();
break;
}
}while(num!=4);
return0;
}
Q#6: Write a "checkbook" class. You put a list of numbers into this class and get a total out.
Member functions:
void check::additem(int amount); // Add a new entry to the checkbook
int check::total(void); // Return the total of all items
Program:
#include <iostream>
using namespace std;
Class check
{
Private:
int sum=0,n;
Public:
void add item();
int add item(int amount);
total(void);
};
void check::additem(){
cout<<"Enter number of items in the data set:";
cin>>n;
int dset[n];
int i;
for(i=0;i<n;i++) {
cout<<"dset["<<i<<"]:";
cin>>dset[i];
}
}
void check::total(){
for(i=0;i<n;i++){
sum=sum+dset[i];
cout<<"Total:"<<sum<<endl;
}
}
int main(){
check c;
void a;
int t;
a=c.additem();
t=c.total();
cout << "numbers:" << a << endl;
cout << "sum:" << t << endl;
return 0;
}
Method 2:
#include <iostream>
#include <conio.h>
using namespace std;
void sumArray(){
int sum=0,n;
cout<<"Enter number of items in the data set:";
cin>>n;
int dset[n];
int i;
for(i=0;i<n;i++) { //input array elements
cout<<"dset["<<i<<"]:";
cin>>dset[i];
}
for(i=0;i<n;i++)
sum=sum+dset[i];
cout<<"Total:"<<sum<<endl;
}
int main(){
sumArray();
getch();
return 0;
}

Mais conteúdo relacionado

Mais procurados

Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
mussawir20
 

Mais procurados (20)

Ch 7 data binding
Ch 7 data bindingCh 7 data binding
Ch 7 data binding
 
Python-Functions.pptx
Python-Functions.pptxPython-Functions.pptx
Python-Functions.pptx
 
Shared preferences
Shared preferencesShared preferences
Shared preferences
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
 
[Question Paper] Object Oriented Programming With C++ (Revised Course) [April...
[Question Paper] Object Oriented Programming With C++ (Revised Course) [April...[Question Paper] Object Oriented Programming With C++ (Revised Course) [April...
[Question Paper] Object Oriented Programming With C++ (Revised Course) [April...
 
SQL: Creating and Altering Tables
SQL: Creating and Altering TablesSQL: Creating and Altering Tables
SQL: Creating and Altering Tables
 
Binary search
Binary searchBinary search
Binary search
 
Methods In C-Sharp (C#)
Methods In C-Sharp (C#)Methods In C-Sharp (C#)
Methods In C-Sharp (C#)
 
NFA & DFA
NFA & DFANFA & DFA
NFA & DFA
 
Controls in asp.net
Controls in asp.netControls in asp.net
Controls in asp.net
 
Ppt of dbms e r features
Ppt of dbms e r featuresPpt of dbms e r features
Ppt of dbms e r features
 
Function overloading and overriding
Function overloading and overridingFunction overloading and overriding
Function overloading and overriding
 
Unit 5 composite datatypes
Unit 5  composite datatypesUnit 5  composite datatypes
Unit 5 composite datatypes
 
Inheritance in Java
Inheritance in JavaInheritance in Java
Inheritance in Java
 
C# Access modifiers
C# Access modifiersC# Access modifiers
C# Access modifiers
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 
Php Using Arrays
Php Using ArraysPhp Using Arrays
Php Using Arrays
 
Content provider in_android
Content provider in_androidContent provider in_android
Content provider in_android
 
[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member[OOP - Lec 18] Static Data Member
[OOP - Lec 18] Static Data Member
 

Semelhante a OOP program questions with answers

C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
Mohamed Ahmed
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
Sidd Singh
 

Semelhante a OOP program questions with answers (20)

C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
Assignment Java Programming 2
Assignment Java Programming 2Assignment Java Programming 2
Assignment Java Programming 2
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
P3
P3P3
P3
 
Array Cont
Array ContArray Cont
Array Cont
 
Functions
FunctionsFunctions
Functions
 
Python Lecture 4
Python Lecture 4Python Lecture 4
Python Lecture 4
 
Lecture2.ppt
Lecture2.pptLecture2.ppt
Lecture2.ppt
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
friends functionToshu
friends functionToshufriends functionToshu
friends functionToshu
 
Operating system labs
Operating system labsOperating system labs
Operating system labs
 
C++ Functions.ppt
C++ Functions.pptC++ Functions.ppt
C++ Functions.ppt
 
power point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions conceptspower point presentation on object oriented programming functions concepts
power point presentation on object oriented programming functions concepts
 
12th CBSE Practical File
12th CBSE Practical File12th CBSE Practical File
12th CBSE Practical File
 
C++ manual Report Full
C++ manual Report FullC++ manual Report Full
C++ manual Report Full
 
chapter-8-function-overloading.pdf
chapter-8-function-overloading.pdfchapter-8-function-overloading.pdf
chapter-8-function-overloading.pdf
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#
 
Computer Programming- Lecture 10
Computer Programming- Lecture 10Computer Programming- Lecture 10
Computer Programming- Lecture 10
 
python lab programs.pdf
python lab programs.pdfpython lab programs.pdf
python lab programs.pdf
 
Function in c program
Function in c programFunction in c program
Function in c program
 

Último

+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 

OOP program questions with answers

  • 1. Assignment #1 Q#1: Create a class that imitates part of the functionality of the basic data type int. Call the class Int (note different capitalization). The only data in this class is an int variable. Include member functions to initialize an Int to 0, to initialize it to an int value, to display it (it looks just like an int), and to add two Int values. Write a program that exercises this class by creating one uninitialized and two initialized Int values, adding the two initialized values and placing the response in the uninitialized value, and then displaying this result. Program: #include <iostream> usingnamespace std; classInteger { private: int num,num1; public: Integer() {} Integer(intn) { num=n; } int add(IntegerInt1,IntegerInt2) { num1=Int1.num+Int2.num; returnnum1; } }; intmain() { int number; IntegerInt1(10),Int2(13),Int3; number=Int3.add(Int1,Int2); cout<<"1st number:"<<10<<"n2nd number:"<<13<<endl; cout<<"n3rd numberissum of 1st and2nd numbers:"<<number<<"n"; } Output: 1st number:10 2nd number:13 3rd numberissumof 1st and 2nd numbers: 23
  • 2. Q#2: Imagine a tollbooth at a bridge. Cars passing by the booth are expected to pay a 50 cent toll. Mostly they do, but sometimes a car goes by without paying. The tollbooth keeps track of the number of cars that have gone by, and of the total amount of money collected. Model this tollbooth with a class called tollBooth. The two data items are a type unsigned int to hold the total number of cars, and a type double to hold the total amount of money collected. A constructor initializes both of these to 0. A member function called payingCar() increments the car total and adds 0.50 to the cash total. Another function, called nopayCar(), increments the car total but adds nothing to the cash total. Finally, a member function called display() displays the two totals. Make appropriate member functions. Program: #include <iostream> usingnamespace std; classtollboth { private: int null,pcar,ncar; floattax; public: tollboth() { tax=0; pcar=0; ncar=0; null=0; } voidpaycar(inta) { pcar=pcar+a; for(null;null<=pcar;null++) { tax=tax+0.50; } } voidnopaycar(intb) { ncar=ncar+b; }( voiddisplay() { cout<<"Total no of payedcars are : "<<pcar<<endl; cout<<"Total tax is : "<<tax<<endl; cout<<"Total no of not payedcars are : "<<ncar<<endl; } };
  • 3. intmain() { tollbothtb; char press,input; int a,b; do{ cout<<"Press1 forcar pay tax"<<endl; cout<<"Press2 forcar not pay tax"<<endl; cout<<"Press3 fortotal tax and Exit"<<endl; cin>>press; switch(press) { case '1': { cout<<"EnterNo of the cars pay tax"<<endl; cin>>a; tb.paycar(a); break; } case '2': { cout<<"EnterNo of cars notpay tax"<<endl; cin>>b; tb.nopaycar(b); break; } } cout<<"Pressy to continue andn forterminate"<<endl; cin>>input; } while(input=='y'); tb.display(); system("pause"); return0; } Output: Press 1 for car pay tax Press2 forcar not paytax Press3 fortotal tax and exit 1 Enter Noof the cars pay tax 2 Pressy to continue andnfor terminate y Press 1 for car pay tax Press2 forcar not paytax
  • 4. Press3 fortotal tax and exit 2 Enter Noof cars not pay tax 2 Pressy to continue andnfor terminate y Press 1 for car pay tax Press2 forcar not paytax Press3 fortotal tax and exit 3 Pressy to continue andnfor terminate Y Total noof payedcars are : 2 Total tax is : 1.5 Total noof notpayedcars are : 2 Q#3: 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. Another member function should display it, in 11:59:59 format. The final member function should add two objects of type time passed as arguments. A main() program should create two initialized time objects (should they be const?) and one that isn’t initialized. Then it should add the two initialized values together, leaving the result in the third time variable. Finally it should display the value of this third variable. Make appropriate member functions const. Program: #include <iostream> #include <conio.h> usingnamespace std; classtime{ private: inthours,minutes,seconds; public: time(){ hours= minutes=seconds= 0; } time(inth,intm,int s){ hours= h; minutes=m; seconds= s; } voidshowTime() const{ cout << hours<< ':' << minutes<<':' << seconds; } voidaddTime(timex,time y){ seconds= x.seconds+y.seconds;
  • 5. if(seconds>59){ seconds-=60; minutes++; } minutes+= x.minutes+y.minutes; if(minutes>59){ minutes-=60; hours++; } hours+=x.hours+y.hours; } }; intmain(){ const time a(2,23,45), b(4,25,15); time c; c.addTime(a,b); c.showTime(); } Output: 06:49:00 Q#4: Create an employee class. The member data should comprise an int for storing the employee number and a float for storing the employee’s compensation. Member functions should allow the user to enter this data and display it. Write a main() that allows the user to enter data for three employees and display it. Program: #include <iostream> #include <conio.h> usingnamespace std; classemployee{ private: intemp_num; floatemp_comp; public: voidentData(){ cout << "EnterEmployee'sNumber: "; cin >> emp_num; cout << "EnterEmployee'sSalary :" ; cin >> emp_comp; } voiddisplay(){ cout << "Employee'sNumber:" << emp_num<< endl; cout << "Enployee'sSalary: "<< emp_comp<< endl;
  • 6. } }; intmain(){ employee emp1,emp2,emp3; cout << "Enter Data For Employee 1"<< endl; emp1.entData(); cout << "Enter Data For Employee 2"<< endl; emp2.entData(); cout << "Enter Data For Employee 3"<< endl; emp3.entData(); cout << "Total Data EnteredIs : " << endl; emp1.display(); emp2.display(); emp3.display(); } Output: Enter Data For Employee 1 Enter Employee’sNumber:5 Enter Employee’sSalary:20000 Enter Data For Employee 2 Enter Employee’sNumber:6 Enter Employee’sSalary:65000 Enter Data For Employee 3 Enter Employee’sNumber:7 Enter Employee’sSalary:15000 Total Data EnteredIs: Employee’sNumber 5 Employee’sSalary 20000 Employee’sNumber 6 Employee’sSalary 65000 Employee’sNumber 7 Employee’sSalary 15000 Q#5: Define a class that will hold the set of integers from 0 to 31. An element can be set with the set member function and cleared with the clear member function. It is not an error to set an element that's already set or clear an element that's already clear. The function test is used to tell whether an element is set. Program: #include <iostream> #include<windows.h> #include<conio.h> usingnamespace std; classInteger{
  • 7. public: intnumber_array[34]; voidset(intloc,intnum){ number_array[loc]=num; } voidclear(){ for(inti=0;i<=31;i++){ number_array[i] =0; } } voidsetArray(){ for(intk=0;k<=31;k++) { number_array[k] =k; } cout<<"nnCongratulations....nnyouhave succesfully StoredVALUESinARRAY..!"; getch(); } voidtest(){ intcount=0; for(intj=0;j<=31;j++) { if(number_array[j]==0){ count++; } } if(count>20){ cout<<"array is empty"; } else { for(intj=0;j<=31;j++){ cout<<number_array[j]<<","; } } getch(); } }; intmain() { // yourcode goeshere intnumb,num;
  • 8. Integera; do { system("cls"); cout<<"nnntMAIN MENU"; cout<<"nnt01.Set Array1 to 31 values"; cout<<"nnt02.Test Array"; cout<<"nnt03.ClearArray"; cout<<"nnt04.EXIT"; cout<<"nntSelectYourOption(1-4) "; cin>>num; switch(num) { case 1: a.setArray(); break; case 2: a.test(); break; case 3: a.clear(); break; } }while(num!=4); return0; } Q#6: Write a "checkbook" class. You put a list of numbers into this class and get a total out. Member functions: void check::additem(int amount); // Add a new entry to the checkbook int check::total(void); // Return the total of all items Program: #include <iostream> using namespace std; Class check { Private: int sum=0,n; Public:
  • 9. void add item(); int add item(int amount); total(void); }; void check::additem(){ cout<<"Enter number of items in the data set:"; cin>>n; int dset[n]; int i; for(i=0;i<n;i++) { cout<<"dset["<<i<<"]:"; cin>>dset[i]; } } void check::total(){ for(i=0;i<n;i++){ sum=sum+dset[i]; cout<<"Total:"<<sum<<endl; } } int main(){ check c; void a;
  • 10. int t; a=c.additem(); t=c.total(); cout << "numbers:" << a << endl; cout << "sum:" << t << endl; return 0; } Method 2: #include <iostream> #include <conio.h> using namespace std; void sumArray(){ int sum=0,n; cout<<"Enter number of items in the data set:"; cin>>n; int dset[n]; int i; for(i=0;i<n;i++) { //input array elements cout<<"dset["<<i<<"]:"; cin>>dset[i]; }