### Archive

Posts Tagged ‘CLASS Access Specifiers’

## C++ Program to implement Toll Tax Problem by using CLASS Access Specifiers and SWITCH CASE – Q12

Q12. Program to implement Toll Tax Problem by using CLASS Access Specifiers and SWITCH CASE:

Calculate the toll tax for cars passing by a toll bridge @ Rs 5 per car (use the concept static data members). Calculate and Print the following details:
– Total no of cars passed with paying the tax
– Total amount of tax paid
– Total no of cars passed without paying tax.

… from College notes (BCA/MCA assignments):

```#include <iostream.h>
#include <conio.h>
#include <stdlib.h>

class TollBridge{
private:
static int car_yes, car_no;
static int amt;
public:
static void GetCar();
static void PutCar();
};

int TollBridge::car_yes = 0;
int TollBridge::car_no = 0;
int TollBridge::amt = 0;

void TollBridge :: GetCar(){
char ch;
cout<<"\n Total Cars Passed: "<<car_yes+car_no;
cout<<"\n This Car:- ";
cout<<"\n Paid Tax (y/n) ?: ";
cin>>ch;
if(ch == 'y'){
car_yes++;
amt += 5;
}
else
car_no++;
}

void TollBridge :: PutCar(){
cout<<"\n Total Car Passed: "<<car_yes+car_no<<endl;
cout<<"\n\t Car Passed by giving Tax: "<<car_yes;
cout<<"\n\t\t Amount Recieved: "<<amt<<endl;
cout<<"\n Car Passed by not giving Tax: "<<car_no;
}

void main(){
int ch;
char choice;
while(1){
clrscr();
cout<<"\n TOLL TAX PLAZA";
cout<<"\n ~~~~~~~~~~~~~~";
cout<<"\n 1 -> Entry of CARS.";
cout<<"\n 2 -> Report of CARS.";
cout<<"\n 3 -> Exit.";
cin>>ch;
switch(ch){
case 1:
while(1){
TollBridge::GetCar();
cout<<"\n Do you want to continue
(y/n) ?: ";
cin>>choice;
if(choice =='n') break;
}
break;
case 2:
TollBridge::PutCar();
getch();
break;
default:
exit(1);
} // end of switch.
} // end of while.
} // end of main.
```

Output:

TOLL TAX PLAZA
1 -> Entry of CARS.
2 -> Report of CARS.
3 -> Exit.

Total Cars Passed: 0
This Car:-
Paid Tax (y/n) ?: y

Do you want to continue (y/n) ?: y

Total Cars Passed: 1
This Car:-
Paid Tax (y/n) ?: y

Do you want to continue (y/n) ?: y

Total Cars Passed: 2
This Car:-
Paid Tax (y/n) ?: y

Do you want to continue (y/n) ?: y

Total Cars Passed: 3
This Car:-
Paid Tax (y/n) ?: n

Do you want to continue (y/n) ?: y

Total Cars Passed: 4
This Car:-
Paid Tax (y/n) ?: n

Do you want to continue (y/n) ?: y

Do you want to continue (y/n) ?: n

TOLL TAX PLAZA
1 -> Entry of CARS.
2 -> Report of CARS.
3 -> Exit.

Total Car Passed: 5
Car Passed by giving Tax: 3
Amount Recieved: 15
Car Passed by not giving Tax: 2

Categories: Cpp

## C++ Program to work with Purchase Order Problem by using Pointers and CLASS with PUBLIC & PRIVATE Access Specifiers – Q10

Q10. Program to work with Purchase Order Problem:

Solve the following purchase order problem:
– Item list entry
– Bill generation
– Modify Item quantity purchased
– Remove Item from the list

… from College notes (BCA/MCA assignments):

```#include <iostream.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>

class CBills{
private:
char name[20];
int qty;
float price, net;
public:
void Getinfo();
void Modify(CBills *, int );
void Remove(CBills *, int &);
void Display() const;
};

void CBills :: Getinfo(){
cout<<"\n Enter Item Name: ";
cin>>name;
cout<<"\n Enter Item Price: ";
cin>>price;
cout<<"\n Enter Item Quantity: ";
cin>>qty;
net = price * qty;
}

void CBills :: Modify(CBills *order, int no){
char key[15];
cout<<"\n Enter the Item Name to Modify: ";
cin>>key;
for(int i=0; i<=no; i++){
if(strcmp(order[i].name, key) == 0)
break;
}
cout<<"\n Enter Item Name: ";
cin>>order[i].name;
cout<<"\n Enter Item Price: ";
cin>>order[i].price;
cout<<"\n Enter Item Quantity: ";
cin>>order[i].qty;
order[i].net = order[i].price * order[i].qty;
}

void CBills :: Remove(CBills *order, int &no){
char key[15];
cout<<"\n Enter the Item Name to Remove: ";
cin>>key;
for(int i=0; i<=no; i++){
if(strcmp(order[i].name, key) == 0)
break;
}
for(int j=i; j<=no; j++)
order[j] = order[j+1];
no--;
}

void CBills :: Display() const{
cout<<"\n\t Item Name:  "<<name;
cout<<"\n\t Item Price: "<<price;
cout<<"\n\t Item Quantity: "<<qty;
cout<<"\n\t Net Price: "<<net;
}

void main(){
CBills bills[20], bl;
int ch1, item_no = -1;
char ch2;
while(1){
clrscr();
cout<<"\n Inventory and Purchase Order Managament.";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout<<"\n 1 -> Get Item Details.";
cout<<"\n 2 -> Display Items.";
cout<<"\n 3 -> Modify an Item.";
cout<<"\n 4 -> Remove an Item.";
cout<<"\n 5 -> Exit.";
cin>>ch1;
switch(ch1){
case 1: // Getinfo.
while(1){
clrscr();
item_no++;
cout<<"\n Entry of Purchased Items.";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~~~";
cout<<"\n Item No. "<<item_no+1;
bills[item_no].Getinfo();
cout<<"\n Do you want to add more
(y/n): ";
cin>>ch2;
if(ch2 == 'n')
break;
}
break;
case 2: // Display.
clrscr();
cout<<"\n Purchase Order Details.";
cout<<"\n ~~~~~~~~~~~~~~~~~~~~~~~";						for(int i=0; i<=item_no; i++){
cout<<"\n\n Item No.: "<<i+1;
bills[i].Display();
}
break;
case 3: // Modify.
bl.Modify(bills, item_no);
cout<<"\n\n Record Modified.";
break;
case 4: // Remove.
bl.Remove(bills, item_no);
cout<<"\n\n Record Removed.";
break;
default:
exit(1);
} // end of switch.
getch();
} // end of while.
} // end of main.
```

Output:

Inventory and Purchase Order Managament.
1 -> Get Item Details.
2 -> Display Items.
3 -> Modify an Item.
4 -> Remove an Item.
5 -> Exit.

Entry of Purchased Items.

Item No. 1
Enter Item Name: Magazine

Enter Item Price: 100

Enter Item Quantity: 5

Do you want to add more (y/n): y

Entry of Purchased Items.

Item No. 2
Enter Item Name: Register

Enter Item Price: 50

Enter Item Quantity: 10

Do you want to add more (y/n): y

Entry of Purchased Items.

Item No. 3
Enter Item Name: Pen

Enter Item Price: 10

Enter Item Quantity: 20

Do you want to add more (y/n): n

```Purchase Order Details.
Item No.	Name		Price		Qty		Net Price
1		Magazine	100		5		500
2		Register	50		10		500
3		Pen		10		20		200
```

Categories: Cpp

## C++ Program to maintain a Cricket Team by using CLASS with PUBLIC & PRIVATE Access Specifiers – Q9

Q9. Program to maintain a Cricket Team:

Make a Class Cricket_Team with following attributes:
– Team Name
– Player Name
– Batting Average
Read the details various players from different team in random.
Now print the team wise list of players in the sorted order of their batting averages.

Use functions:
– To read the player detail
– To arrange the players team wise
– To sort the batting average
– To Print the details

… from College notes (BCA/MCA assignments):

```#include <iostream.h>
#include <iomanip.h>
#include <string.h>
#include <conio.h>

class CCricket{
private:
char country[20], player[20];
int avg;
public:
void getTeams();
void compTeams(CCricket *, const int);
void dispTeams(const CCricket *, const int);
};

void CCricket :: getTeams(){
cout<<"\n Enter the Name of a Country: ";
cin>>country;
cout<<"\n Enter a Player Name: ";
cin>>player;
cout<<"\n Enter the Batting Average: ";
cin>>avg;
}

void CCricket :: compTeams(CCricket *Ock, const int t_pls){
int i, j;
CCricket Otemp;
// Sorting By Players Name.
for(i=0; i<=t_pls; i++){
for(j=i+1; j<=t_pls; j++){
if(Ock[i].avg < Ock[j].avg){
Otemp = Ock[i];
Ock[i] = Ock[j];
Ock[j] = Otemp;
}
}
}
// Sorting By Country Name.
for(i=0; i<=t_pls; i++){
for(j=i+1; j<=t_pls; j++){
if(strcmp(Ock[i].country, Ock[j].country) > 0){
Otemp = Ock[i];
Ock[i] = Ock[j];
Ock[j] = Otemp;
}
}
}
}

void CCricket :: dispTeams(const CCricket *Ock, const int t_pls){
int i, j;
char t_c_name[10];
// Display Players.
cout<<"\n\n Players Sorted According to their Country and
Average:- \n";
cout<<"\n COUNTRY \t TEAM \t AVERAGE"<<endl;
for(i=1; i<=t_pls; i++){
if(strcmp(t_c_name, Ock[i].country) != 0){
cout<<"\n "<<Ock[i].country;
strcpy(t_c_name, Ock[i].country);
}
cout<<"\n\t\t"<<Ock[i].player<<"  -  "
<<setw(5)<<Ock[i].avg<<endl;
}
}

void main(){
int i=0;
char ch;
CCricket Ock[30], Otemp;

while(1){
clrscr();
Ock[i].getTeams();
i++;
cout<<"\n Do you want to Enter next Entry (y/n) ? : ";
cin>>ch;
if(ch == 'n')
break;
} // End of while Loop.

cout<<"\n\n Total Players Entered: "<<i<<endl;

// Sort Teams.
Otemp.compTeams(Ock, i);

// Display Teams.
Otemp.dispTeams(Ock, i);

getch();
}
```

Output:

Enter the Name of a Country: India

Enter a Player Name: Sachin

Enter the Batting Average: 70

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: Australia

Enter a Player Name: Ponting

Enter the Batting Average: 50

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: Bang

Enter a Player Name: Zahid

Enter the Batting Average: 30

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: India

Enter a Player Name: Rahul

Enter the Batting Average: 45

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: Australia

Enter a Player Name: Gilcrist

Enter the Batting Average: 75

Do you want to Enter next Entry (y/n) ? : y

Enter the Name of a Country: India

Enter a Player Name: Harbhjan

Enter the Batting Average: 50

Do you want to Enter next Entry (y/n) ? : n

Total Players Entered: 6

Players Sorted According to their Country and Average:-

COUNTRY TEAM

Australia
Gilcrist – 75

Ponting – 50

Bang
Zahid – 30

India
Harbhjan – 50

Rahul – 45

Sachin – 70

Categories: Cpp

## C++ Program to maintain Student Result by using CLASS with PUBLIC & PRIVATE Access Specifiers – Q7

January 7, 2010 1 comment

Q7. Program Matrix manipulation (menu based):

Make a structure Student with following fields:
– Name
– Roll No.
– Result (Structure)
– – Total Marks Obtained
– – Max_Marks
– – Percentage

Read the list of students (At least 3) in a class.
Now compare the results of all the students and Rank top 3 students in the whole class.

… from College notes (BCA/MCA assignments):

```#include <iostream.h>
#include <conio.h>

class CStud{
private:
char name[20], course[10];
public:
int  marks;
void getinfo();
void compute();
void display();
};

void CStud :: getinfo(){
cout<<"\n Enter Name: ";
cin>>name;
cout<<"\n Enter Course: ";
cin>>course;
cout<<"\n Enter Marks Obtained (out of 500): ";
cin>> marks;
}

void CStud :: compute(){
int per;
per = marks/5;
if(per >= 80)
else if( (per < 80) && (per >= 60) )
else if( (per < 60) && (per >= 40) )
else
}

void CStud :: display(){
cout<<"\n Name: "<<name;
cout<<"\t Course: "<<course;
cout<<"\t Marks: "<<marks;
}

void main(){
int n = 0;
char ch;

CStud Ostu[10];				// Creating Object.

clrscr();

while(ch != 'n'){               	// Getting Information.
gotoxy(25, 1);

cout<<"-: STUDENT INFORMATION :- \n";
cout<<"\n Student No.: "<<n+1<<endl;
Ostu[n].getinfo();
cout<<"\n Next Student (y/n): ";
cin>>ch;
n++;
clrscr();
}

// Sorting.
CStud Otemp;
for(int i=0; i<n; i++){
for(int j=i; j<n; j++){
if(Ostu[i].marks < Ostu[j].marks){
Otemp = Ostu[i];
Ostu[i] = Ostu[j];
Ostu[j] = Otemp;
}
}
}

clrscr();
gotoxy(25, 1);
cout<<"-: STUDENT'S REPORT :- ";		// Display Results.
for(i=0; i<n; i++){
cout<<"\n\n Student No.: "<<i+1;
Ostu[i].display();
}

cout<<"\n\n Press Any Key to Exit ...";
getch();
}
```

Output:

-: STUDENT INFORMATION :-

Student No.: 1

Enter Name: Manoj

Enter Course: MCA

Enter Marks Obtained (out of 500): 350

Next Student (y/n): y

-: STUDENT INFORMATION :-

Student No.: 2

Enter Name: Bhanu

Enter Course: MBA

Enter Marks Obtained (out of 500): 400

Next Student (y/n): n

-: STUDENT’S REPORT :-

Student No.: 1
Name: Bhanu Course: MBA Marks: 400 Grade: A

Student No.: 2
Name: Manoj Course: MCA Marks: 350 Grade: B

Press Any Key to Exit …

Categories: Cpp