Anda di halaman 1dari 26

1

Certificate
Name:
Class & Section:

Rohan Gupta
XII A

School:

Lucknow Public School

Session:

2015-2016

This is to be certified to be the bonafide work of the student of


computer laboratory during academic session 2015-2016.
Project work is successfully completed in the computer practical for the
AISSCE.
As prescribed by CBSE in the year 2015-2016.

____________________

___________________

Examiner`s Signature

Teacher`s Signature

Acknowledgement
I am extremely grateful and remain indebted to my teacher & guide Mrs. Narayani Singh for
being a source of inspiration and for her constant support in the design, implementation and
evaluation of the project. I am thankful to her constant criticism and invaluable suggestion,
which benefitted us a lot while developing the project. She has been a constant source of
inspiration and motivation for hard work & has been very co-operative throughout this project
work.
I also express my gratitude to my principal, Ms. Bharti Gosain for providing us the
infrastructure to carry out the project and to all the staff members who were directly &
indirectly instrument in enabling us to stay committed for the project. At last I would like to
thank my parents who helped me in various ways with their unconditional support and

affection during the years and always being there for me. They inspired my every step ahead to
put in my best.

Contents

Introduction
The C++ programming language was developed at AT&T Bell laboratories in the early 1980s by

Bjarne Stroustrup. He found C lacking for simulations and decided to extend the language by
adding features form his favorite language, Simula 67. Simula 67 was one of the earliest object
oriented language. Bjarne Stroustroup called it C with classes or New C during
development. The name C++ (pronounced C plus plus) was coined by Rick Mascitti where
++is the C increment operator. Ever since its birth, C++ evolved to cope with problems

encountered by users, and through discussions at AT&T. However, the maturation of the C++
language is attested by two recent events:
(i)The formation of an ANSI (American National Standard Institute)
And (ii) the publication of The Annotated C+ Reference

Manual by Ellis and Stroustrup.


Many other programming languages have been influenced by C++, including C#, Java, and
newer versions of C (after 1998).

Function Overloading
Program 1: To illustrate the working of Function Overloading. Calculating
interest amount.
#include<iostream.h>
#include<stdlib.h>
void amount(float princ, int time, float rate)
{
cout<<"\nPrincipal Amount:"<<princ;
cout<<"\tTime:"<<time<<"year";
cout<<"\tRate:"<<rate;
cout<<"\nInterest Amount:"<<(princ*time*rate)<<"\n";
}
void amount(float princ, int time)
{
cout<<"\nPrincipal Amount:"<<princ;
cout<<"\tTime:"<<time<<"years";
cout<<"\tRate: 0.08";
cout<<"\nInternet Amount:"<<(princ*time*0.08)<<"\n";
}
void amount(float princ, float rate)
{
cout<<"\nPrincipal Amount:"<<princ;
cout<<"\tTime : 2 year";
cout<<"\tRate:"<<rate;
cout<<"\nInterest Amount:"<<(princ*2*rate)<<"\n";

6
}
void amount(int time, float rate)
{
cout<<"\nPrincipal Amount: 2000";
cout<<"\tTime:"<<time<<"years";
cout<<"\tRate:"<<rate;
cout<<"\nInterest Amount:"<<(2000*time*rate)<<"\n";
}
void amount(float princ)
{
cout<<"\nPrincipal Amount:"<<princ;
cout<<"\tTime: 2 years";
cout<<"\tRate: 0.08";
cout<<"\nInterest Amount:"<<(princ*2*0.08)<<"\n";
}
int main()
{
system("cls");
cout<<"Case 1";
amount(2000.0F);
cout<<"Case 2";
amount(2500.0F,3);
cout<<"Case 3";
amount(2300.0F,3,0.11F);
cout<<"Case 4";
amount(2,0.12F);
cout<<"Case 5";

7
amount(6,0.07F);
return 0;
}

OUTPUT:

Classes and Objects


Program 2: Using 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<stdlib.h>
class ITEM {
int itemcode[5];
float it_price[5];
public:
void initialize(void);
float largest(void);
float sum(void);
void display_item(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";
}

9
}
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];
}
return sum;
}
void ITEM::display_item(void)
{
cout<<"\nCode Price\n";
for(int i=0;i<5;i++)
{
cout<<"\n"<<itemcode[i];
cout<<"\n"<<it_price[i];

10
}
cout<<"\n";
}
int main()
{
ITEM order;
order.initialize();
float total, biggest;
int ch=0;
system("cls");
do
{
cout<<"\n\tMAIN 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_item();

11
break;
}
}
while(ch>=1&&ch<=3);
return 0;
}

OUTPUT:

12

Program 3: To illustrate the working of a function returning an object.


#include<iostream.h>
class Distance {
int feet, inches;
public:
void getdata(int f,int i)
{feet=f;inches=i;}
void printit(void)
{
cout<<feet<<" Feet

"<<inches<<" Inches

"<<"\n";
}
Distance sum(Distance d2);
};
Distance Distance::sum(Distance d2)
{
Distance d3;
d3.feet=feet+d2.feet+(inches+d3.inches)/12;
d3.inches=(inches+d2.inches)%12;
return (d3);
}
int main()
{
Distance Length1, Length2,total;
Length1.getdata(17,8);
Length2.getdata(14,5);
total=Length1.sum(Length2);

13
cout<<"Length 1: ";
Length1.printit();
cout<<"Length 2: ";
Length2.printit();
cout<<"Total Length: ";
total.printit();
return 0;
}

OUTPUT:

14

Program 4: To illustrate order of constructor invocation


#include<iostream.h>
#include<stdlib.h>
class Subject { int days;
int subjectno;
public:
Subject(int d=123,int sn=101);
void printsub(void)
{
cout<<"Subject No.:"<<subjectno;
cout<<"\n"<<"Days:"<<days<<"\n";
}
};
Subject::Subject(int d,int sn)
{
cout<<"Constructing Subject\n";
days=d;
subjectno=sn;

}
class Student { int rollno;
float marks;
public:
Student()
{cout<<"Constructing Student\n";

15
rollno=0;
marks=0.0;
}
void getval(void)
{
cout<<"Enter Roll Number And Marks: ";

cin>>rollno>>marks;
cout<<"\n";
}
void print(void)
{
cout<<"Roll Number: "<<rollno;

cout<<"\nMarks: "<<marks<<"\n";
}
};
class Admission{
Subject sub;
Student stud;
float fees;
public:
Admission()
{
cout<<"Constructing Admission\n";
fees=0.0;

16
}
void print(void)
{
stud.print();
sub.printsub();
cout<<"Fees: "<<fees<<"\n";

}
};
int main()
{
system("cls");
Admission adm;

cout<<"\nBack in Main()\n";
return 0;
}

OUTPUT:

17

Program 5: To illustrate the use of overloaded Constructor


#include<iostream.h>
#include<stdlib.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=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 Investment:"<<rate;

};

18
cout<<"\tTotal Amount: Rs"<<total_amt<<"\n";

int main()
{

system("cls");
Deposit D1, D2(2000,2,0.0f), 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();
return 0;

OUTPUT:

19

Program 6: To show working of virtual base class.


#include<iostream.h>
#include<conio.h>
class Base
{
public:
int a;
};
class D1:virtual public Base
{
public:
int b;
};
class D2:virtual public Base
{
public:
int c;
};
class D3:public D1,public D2
{
public:
int total;
};
int main()
{
clrscr();
D3 ob;
ob.a=25;
ob.b=50;
ob.c=75;
ob.total=ob.a+ob.b+ob.c;
cout<<"ob.a"<<"\t"<<"ob.b"<<"\t"<<"ob.c"<<"\t"<<"ob.total"<
<endl;
cout<<ob.a<<"\t"<<ob.b<<"\t"<<ob.c<<"\t"<<ob.total<<endl;
getch();
return 0;
}

OUTPUT :

20

Data File Handling


Program 7: To illustrate of working with multiple files simultaneously
#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
int main()
{
system("cls");
ifstream filin1, filin2;
filin1.open("Stuname.txt",ios::in);
filin2.open("Stumarks.txt",ios::in);
char line[80];
cout<<"The Contents of Stunames and Stumarks are given
below. \n";
filin1.getline(line,80);
cout<<line<<"\n";
filin2.getline(line,80);
cout<<line<<"\n";
filin1.getline(line,80);
cout<<line<<"\n";
filin2.getline(line,80);
cout<<line<<"\n";
filin1.getline(line,80);
cout<<line<<"\n";
filin2.getline(line,80);

21
cout<<line<<"\n";
cout<<line<<"\n";
filin1.close();
filin2.close();
return 0;
}

OUTPUT:

22

Program 8: To write and read a structure using write() and read()


function using a binary file
#include<iostream>
#include<fstream>
#include<string.h>
#include<stdlib.h>
struct customer { char name[51];
float balance; };
int main()
{
system("cls");
customer savac;
strcpy(savac.name,"Toby Marshall");
savac.balance=213466.75;
ofstream fout;
fout.open("Saving",ios::out|ios::binary);
if(!fout)
{
cout<<"File can`t be opened\n";
return 1;
}
fout.write((char *)&savac, sizeof(customer));
fout.close();
fstream fin;
fin.open("Saving", ios::in|ios::binary);
fin.read((char *)&savac, sizeof(customer));

23
cout<<savac.name;
cout<<" has the balance amount of Rs."<<savac.balance<<"\n";
fin.close();
return 0;
}

OUTPUT:

24

Program 9: To append data in a file


#include<iostream.h>
#include<fstream.h>
class stu { int rollno;
char name[25];
char Class[4];
float marks;
char grade;
public:
void getdata()
{ cout<<"Roll Number:"; cin>>rollno;
cout<<"Name :"; cin>>name;
cout<<"Class:"; cin>>Class;
cout<<"Marks:"; cin>>marks;
if(marks>=75) grade='A';
else if(marks>=60) grade='B';
else if(marks>=50) grade='C';
else if(marks>=40) grade='D';
else grade='F';
}
void putdata()
{
cout<<name<<", Roll Number "<<rollno<<" has
"<<marks<<" % marks and"<<grade<<" grade "<<endl;
}
int getrno(){return rollno;}
}s1;

25
int main()
{
ofstream fo("stu.dat", ios::app|ios::binary);
char ans='y';
while(ans=='y')
{
s1.getdata();
fo.write((char *) &s1,sizeof(s1));
cout<<"Record added to file \n";
cout<<"Want to enter more record?(y/n)..";
cin>>ans;
}
fo.close();
return 0;
}

OUTPUT:

26

Progra

Anda mungkin juga menyukai