SlideShare a Scribd company logo
1 of 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;
}

More Related Content

What's hot

friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)Ritika Sharma
 
operator overloading
operator overloadingoperator overloading
operator overloadingNishant Joshi
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure shameen khan
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Abdul Hannan
 
Employee management system report
Employee management system reportEmployee management system report
Employee management system reportPrince Singh
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C ProgrammingShuvongkor Barman
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.pptTareq Hasan
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cppgourav kottawar
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptxRAGAVIC2
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods javaPadma Kannan
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++Vishal Patil
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in javaCPD INDIA
 
Stack organization
Stack organizationStack organization
Stack organizationchauhankapil
 

What's hot (20)

single linked list
single linked listsingle linked list
single linked list
 
friend function(c++)
friend function(c++)friend function(c++)
friend function(c++)
 
operator overloading
operator overloadingoperator overloading
operator overloading
 
linked list in data structure
linked list in data structure linked list in data structure
linked list in data structure
 
Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual Object Oriented Programming Lab Manual
Object Oriented Programming Lab Manual
 
Employee management system report
Employee management system reportEmployee management system report
Employee management system report
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt08 c++ Operator Overloading.ppt
08 c++ Operator Overloading.ppt
 
Python projects
Python projectsPython projects
Python projects
 
Constructor ppt
Constructor pptConstructor ppt
Constructor ppt
 
operator overloading & type conversion in cpp
operator overloading & type conversion in cppoperator overloading & type conversion in cpp
operator overloading & type conversion in cpp
 
Array of objects.pptx
Array of objects.pptxArray of objects.pptx
Array of objects.pptx
 
Strings in python
Strings in pythonStrings in python
Strings in python
 
Classes,object and methods java
Classes,object and methods javaClasses,object and methods java
Classes,object and methods java
 
Java essential notes
Java essential notesJava essential notes
Java essential notes
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Nested loops
Nested loopsNested loops
Nested loops
 
Collection Framework in java
Collection Framework in javaCollection Framework in java
Collection Framework in java
 
Stack organization
Stack organizationStack organization
Stack organization
 

Similar to OOP program questions with answers

Similar to 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
 

Recently uploaded

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 

Recently uploaded (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 

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]; }