Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Program using a class to store price list of 5 items and to print the largest price as well as
the sum of all prices.
#include<iostream.h>
#include<conio.h>
class ITEM {
int itemcode[5];
float it_price[5];
public:
void initialize (void);
float largest (void);
float sum (void);
void display_items (void);
};
void ITEM :: initialize (void)
{
for(int i=0; i<5; i++)
{
cout<<”\n”<<Item No:”<<(i+1);
cout<<”\n”<<”Enter item code:”;
cin>>itemcode[i];
cout<<”\n”<<”Enter item price:”;
cin>>it_price[i];
cout<<”\n”;
}
}
float ITEM :: largest (void)
{
float large=it_price[0];
for(int i=1; i<5; i++)
{
if(large<it_price[i])
large=it_price[i];
}
return large;
}
float ITEM :: sum (void)
{
float sum=0;
for(int i=0; i<5;i++)
sum+=it_price[i];
retuirn sum;
}
void ITEM :: display_items (void)
{
cout<<”\nCode Price\n”;
for(int i=0; i<5; i++)
{
cout<<”\n”<<itemcode[i];
cout<<”\n”<<it_price[i];
}
cout<<”\n”;
}
void main()
{
ITEM order;
order.initialize();
float total,biggest;
int ch=0;
clrscr();
do
{
cout<<”\nMain Menu.\n”;
cout<<”\n1.Display largest price.”;
cout<<”\n2.Display sum of prices.”;
cout<<”\n3.Display item list.”;
cout<<”\nEnter your choice (1-3):”;
cin>>ch;
switch(ch)
{
case 1 : biggest=order.largest();
cout<<”The Largest price is”<<biggest<<”\n”;
break;
case 2 : total=order.sum();
cout<<”The sum of prices is”<<total<<”\n”;
break;
case 3 : order.display_items();
break;
default : cout<<”\nWrong choice!\n”;
BREAK:
}
} while(ch>=1&& ch<=3);
}
OUTPUT
Main Menu.
1.Display largest price.
2.Display sum of prices.
3.Display item list.
Enter your choice (1-3):1
The Largest price is 98
Main Menu.
1.Display largest price.
2.Display sum of prices.
3.Display item list.
Enter your choice (1-3):2
The sum of prices is 298
Main Menu.
1.Display largest price.
2.Display sum of prices.
3.Display item list.
Enter your choice (1-3):3
Code Price
101 23
102 44
103 98
104 67
105 66
PROGRAM 2
Programs using classes and objects to stimulate result preparation system for 20
students. The data available for each student includes rollno, name and marks in 3
subjects. The percentage marks and grade are to be calculated from the given
information. The percentage marks are the average marks and the grade is calculated as
follows:
#include<iostream.h>
#include<conio.h>
#include<conio.h>
const int Obj=20;
const int size=3;
class student { int rollno;
char name[21];
float marks[size];
float perc;
char grade;
public:
void getval (void)
{ char ch;
cout<<”Enter Data”;
cout<<”\n”<<Roll No:”;
cin>>rollno;
cin.get(ch);
cout<<”\n”<<”Name:”;
cin.getline(name,21);
for(int i=0; i<size; i++)
{ cout<<”\nMarks for subject”<<(i+1)<<”:”;
cin>>marks[i];
}
cout<<”\n”;
}
void calculate (void);
void prnresult (void);
};
void student :: calculate (void)
{ float total;
total=marks[0]+marks[1]+marks[2];
perc=total/3;
if(perc<50)
grade=’F’;
else if(perc<60)
grade=’D’;
else if(perc<75)
grade=’C’;
else if(perc<90)
grade=’B’;
else
grade=’A’;
}
void student :: prnresult (void)
{
cout<<”\n”;
cout<<”Rollno:”<<rollno”\n”;
cout<<”Name:”;
cout.write(name,21);
cout<<”\n”;
cout<<”Marks in subject 1:”<<marks[0]<<”\n”;
cout<<”Marks in subject 2:”<<marks[1]<<”\n”;
cout<<”Marks in subject3:”<<marks[2]<<”\n”;
cout<<”Total Marks:”<<(marks[0]+marks[1]+[marks[2])<<”\n”;
cout<<”grade:”<<grade<<”\n”;
cout<<”\n”
}
student std10[Obj];
int main()
{ int i=o;
for(i=0; i<Obj; i++)
{ cout<<”Student”<<(i+1)<<”\n”;
Std10[i].getval();
}
for(i=0; i<Obj;i++)
{ std10[i].calculate();
cout<<”Result of students”<<(i+1)<<”\n”;
std10[i].prnresult();
}
return 0;
}
OUTPUT
Student 1
Enter Data
Roll No: 1
Name: Riya
Marks for subject1:76
Marks for subject2:78
Marks for subject 3:78
Student 2
Enter Data
Roll No:2
Name: Shreya
Marks for subject1:77
Marks for subject2:79
Marks for subject 3:80
Student 3
Enter Data
Roll No:3
Name: Ayesha
Marks for subject1:80
Marks for subject2:85
Marks for subject 3:90
Student 4
Enter Data
Roll No:4
Name: Surbhi
Marks for subject1:90
Marks for subject2:87
Marks for subject 3:85
Student 5
Enter Data
Roll No:5
Name: Natasha
Marks for subjectt1:76
Marks for subject2:78
Marks for subject 3:80
Result of student2
Name: Shreya
Marks insubject1:77
Marks in subject2:79
Marks in subject 3:80
Total Marka:236
Grade:B
Result of student
Name: Ayesha
Marks insubject1:80
Marks in subject2:85
Marks in subject 3:85
Total Marks:262
Grade:B
Result of student5
Name:Natasha
Marks insubject1:76
Marks in subject2:78
Marks in subject 3:80
Total Marks:234
Grade:B
PROGRAM 3
#include<iostream.h>
#include<conio.h>
class Deposit { long int principal;
int time;
float rate;
float total-amt;
public:
Deposit();
Deposit(long p, int t, float r);
Deposit(long p, int t);
Deposit(long p, float r);
void calc_amt(void);
void display(void);
};
Deposit::Deposit()
{ principal=time=rate=0.0;}
Deposit::Deposit(long p, int t, float r)
{ principal=p; time=t; rate=r;}
Deposit::Deposit(long p, int t)
{ principal=p; time=t; rate=0.08;}
Deposit::Deposit(long p, float r)
{ principal=p; time=t=2; rate=r;}
void Deposit::calc_amt(void)
{
total_amt=principal+(principal*time*rate)/100
}
void Deposit::display(void)
{ cout<<”\nPrincipal Amount : Rs.”<<principal;
cout<<”\tPeriod of Investment :”<<time<<”years”;
cout<<”\nRate of Interest :”<<rate;
cout<<”\tTotal Amount : Rs.”<<total_amt<<”\n”;
}
void main()
{ clrscr();
Deposit D1, D2(2000,2,0.07f), D3(4000,1), D4(3000,0.12f);
D1.calc_amt();
D2. calc_amt();
D3. calc_amt();
D4. calc_amt();
cout<<”Object 1\n”; D1.display();
cout<<”Object 2\n”; D2.display();
cout<<”Object 3\n”; D3.display();
cout<<”Object 4\n”; D4.display(); }
OUTPUT
Object 1
Principal Amount : Rs.0 Period of Investment : 0 years
Rate of Interest : 0 Total Amount : Rs.0
Object 2
Principal Amount : Rs.2000 Period of Investment : 2 years
Rate of Interest : 0.07 Total Amount : Rs.2280
Object 3
Principal Amount : Rs.4000 Period of Investment : 1 year
Rate of Interest : 0.08 Total Amount : Rs. 4320
Object 4
Principal Amount : Rs.3000 Period of Investment : 2 years
Rate of Interest : 0.12 Total Amount : Rs. 3720
PROGRAM 4
#include<iostream.h>
#include<conio.h>
#include<math.h>
void amount (float princ,int time, float rate
{ cout<<”\n Principal Amount: Rs.”<<princ;
cout<<”\t Time;”<<time<<”years”;
cout<<”\t Rate:”<<rate;
cout<< “\n Interest Amount:”<<(princ*rate*time)<<endl;
}
void amount (float princ,int time)
{ cout<<”\n Principal Amount: Rs.”<<princ;
cout<<”\t Time:”<<time<<”years”;
cout<<”\t Rate:0.08”;
cout<<”\n Interest Amount:”<<(princ*0.08f*time)<<endl;
}
void amount (float princ, float rate)
{ cout<<”\n Principal Amount: Rs.”<<princ;
cout<<”\t Time:”<<2<<”years”;
cout<<”\t Rate:”<<rate;
cout<<”\n Interest Amount:”<<(princ*rate*2)<<endl;
}
void amount (int time. Float rate)
{ cout<<”\n Principal Amount: Rs.”<<2000;
cout<<”\t Time:”<<time<<”years”;
cout<<”\t Rate:”<<rate;
cout<<”\n Interest Amount:”<<(2000*rate*time)<<endl;
}
void amount (float princ)
{ cout<<”\ n Principal Amount: Rs.”<<princ;
cout<<”\t Time:”<<2<<”years”;
cout<<”\t Rate:0.08”;
cout<<”\n Interest Amount:”<<(princ*0.08f*2)<<endl;
}
void main()
{ clrscr();
cout<<”Case 1”;
amount(2000.0f);
cout<<”Case 2”;
amount(2300.0f,3);
cout<<”Case 3”;
amount(2300.0f,3,0.11f);
cout<<”Case 4”;
amount(2000.0f,0.12f);
cout<<”Case 5”;
amount(6,0.07f); }
OUTPUT
Case 1
Principal Amount: Rs.2000 Time: 2 years Rate: 0.08
Interest Amount: Rs.320
Case2
Principal Amount: Rs.2500 Time: 3 years Rate: 0.08
Interest Amount: Rs.600
Case 3
Principal Amount: Rs.2300 Time: 3 years Rate: 0.11
Interest Amount: Rs759
Case 4
Principal Amount: Rs.2000 Time: 2 years Rate: 0.12
Interest Amount: Rs.480
Case 5
Principal Amount: Rs.2000 Time: 6 years Rate: 0.07
Interest Amount: Rs.840
PROGRAM 5
#include<iostream.h>
#include<conio.h>
class Base1 { protected:
int a;
public:
Base1(int x)
{ a=x; cout<<”Constructing Base1 \n”;}
~Base1()
{ cout<<”Destructing Base1 \n”;}
};
class Base2 { protected:
int b;
public:
Base2(int y)
{ b=y; cout<<”Constructing Base2m \n”;}
~Base2()
{ cout<<”Destructing Base2 \n”;}
};
class Derived : public Base2, public Base1
{ int c;
public:
Derived(int i, int j, int k): Base2(i), Base(j)
{
c=k;
cout<<”Constructing Derived.\n”;}
};
~Derived()
{ cout<<”Destructing Derived.\n”;}
void show()
{ cout<<”1.”<<a<<”\t2.”<<b<<”\t3.”<<c<<”\n”;}
};
int main()
{
clrscr();
Derived ob(14, 15, 16);
ob.show();
return 0;
}
OUTPUT
Constructing Base2
Constructing Base1
Constructing Derived
1.15 2.14 3.16
Destructing Derived
Destructing Base1
Destructing Base2
PROGRAM 6
A candidate maintains a list of its students graduating every year. At the end of the year, the college
produces a report that lists the following:
Year_________
Number of Working Graduates :
Number of Non-Working Graduates :
Write a C++ Program for it that uses the following inheritance path:
Person Student Graduate Student
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
const int LEN=25;
class Person { char name[LEN];
int age;
public:
void readperson(void);
void displayperson(void)
{ cout<<”Name:”;
cout.write(name,LEN);
cout<<”\tAge:”<<age<<”\n;
}
};
void Person::readperson(void)
{ for(int i=0; i<LEN; i++) name[i]=’ ‘;
cout<<”Enter Name of the person:”;
gets(name);
cout<<”Enter Age:”;cin>>age;
}
class Student:public Person
{ int rollno;
float average;
public:
void readstudent(void)
{ readperson();
cout<<”Enter Roll number:”;cin>>rollno;
cout<<”Enter Average Marks:”;cin>>average;
}
void disp_rollno(void)
{ cout<<”Roll Number:”<<rollno<<”\n”;}
float getaverage (void)
{ return average;}
};
class GradStudent:public Student
{ char subject[LEN];
char working;
public:
void readit(void);
void displaysubject(void)
{ cout<<”Subject:”;
cout.write(subject,LEN);
}
char workstatus(void)
{ return working;}
};
void GradStudent::readit(void)
{ readstudent();
for(int i=0; i<LEN; i++) subject[i]=’ ‘;
cout<<”Enter Main Subject:”;
gets(subject);
cout<<”Working?(Y/N):”;cin>>working;
}
void main()
{ const int size=5;
GradStudent grad[size];
int year, num_working=0, non_working=0, div1=0,total=0;
float topscore=0,score,number,wperc,nwperc;
cout<<””Enter Year:”;cin>>year;
for(int i=0; i<size; i++)
{ cout<<”Enter Details for Graduate”<<(i+1)<<”\n”;
grad[i].readit();
total++;
if((grad[i].workstatus()==’y’)||(grad[i].workstaus()==’Y’))
num_working++;
else non_working++;
score=grad[i].getaverage();
if(score>topscore)
{ topscore=score; number=l;
]
if(score>=60.0) div1++;
}
l=number;
cout<<”\n”<<”\t\tReport for the Year”<<year<<”\n”;
cout<<”\t\t------------------------------------------\n”;
cout<”Working Graduates:”<<num_working;
cout<<”\tNon-workimg Graduates:”<<non_working<<”\n”;
cout<<”\tDetails ot the Top Scorer\n”;
grad[i].displayperson();
grad[i].displaysubject();
nwperc=((float)non_working/(float)total)*100;
wperc=((float)div1/(float)total)*100;
cout<<”\tAverage Marks:”<<grad[i].getaverage()<<”\n”;
cout<<’\t’<<nwperc<<”% of the graduates this year are non_working and \n”
<<wperc,,”% are first divisioners|n”;
}
OUTPUT