Sunteți pe pagina 1din 16

Prog. #includeiostream.h #includeconio.

h Class b { Int a; Public: Int b; Void get_ab(); Int get_a; Void show_a(); }; Class d:public b { Int c; Public: Void mul (); Void display (); }; Void b::get_ab () { A=5; B=10; } Void b::get_a() { Return a; } Void d::mul () { C=d*get_a (); } Void d::display() { Cout<<a=<<get_a()<<endl; Cout<<b=<<b<<endl; Cout<<c=<<c<<endl<<endl; } Void main () { D d1; D1.get_ab (); D1.mul (); D1.show_a ();

D1.display (); D1.b=20; D1.mul (); D1.display (); Getch (); } Prog. #includeiostream.h #includeconio.h Class b { Int a; Public: Int b; Void get_ab(); Int get_a; Void show_a(); }; Class d:private b { Int c; Public: Void mul (); Void display (); }; Void b::get_ab () { A=5; B=10; } Void b::get_a() { Return a; } Void d::mul () { C=d*get_a (); } Void d::display () { Get_ab(); Show_a(); Cout<<a=<<get_a()<<endl; Cout<<b=<<b<<endl;

Cout<<c=<<c<<endl<<endl; } Void main () { D d1; D1.mul (); D1.display (); D1.mul (); D1.display (); Getch (); }

Pointer-to-object:Prog. #includeiostream.h Class item { Int code; Float price; Public: Void getdata (int a, float b) { code=a; price=b; } Void show() { Cout<<code:<<code<<\n; Cout<<price:<<price<<\n; } }; Void main () { Item *p=new item [2]; Item *d=p; Int x,i; Float y; For (int i=0; i<2; i++) { Cout<<input code & price of the item<<i+1; Cin>>x>>y; p->getdata (x, y); p++; } For (i=0; i<2; i++)

{ Cout<<item:<<i+1<<\n; d->show(); d++; } } Prog. #includeiostream.h #includestring.h Class city { Char *name; Int len; Public: City () { Len=0; Name=new char [len+1]; } Void getname () { Char *s; Cout<<enter city name:; Cin>>s; Len=strlen(s); Name=new char [len+1]; Strcpy (name s); } Void printname () { Cout<<name<<\n; } }; Void main () { City *cptr [10]; Int n=I; Int option; Do { Cptr[n]=new city; Cptr[n]->getname (); N++; Cout<<do you want to enter another city?\n;

Cin>>option; } While (option); Cout<<\n\n; For (int i=1; i<=n;i++) Optr[i]->printname (); }

The this operator:Prog. #includeiostream.h #includestring.h Class person { Char name [20]; Float age; Public: Person (char *s, float s) { Strcpy (name, s); Age=a; } Person & person::greater (person & a) { If (x. age>=age) Return x; Else Return *this; } Void display () { Cout<<name:<<name<<\n<<age:<<age<<\n; } }; Void main() { Person P1 (john, 37.50), P2 (ahmed, 29.0), P3 (hebber, 40.25); Person P (\0,0); P=p1.greater (P3); Cout<<elder person is:\n; P.display ();

P=p1. greater (P2); Cout<<elder person is:\n; p. display (); }

Pointers-to-derived objects:Prog. #includeiostream.h Class BC { Public: Int b; Void show () { cout<<b=<<b<<\n; } }; Class DC: public BC { Public: Int d; Void show () { cout<<b=<<b<<\n<<d=<<d<<\n; } }; Void main () { BC *bptr; BC base; Bptr=&base; Bptr->b=100; Cout<<bptr points to base objects\n; Bptr->show (); DC*dptr; Dptr=&derived; Dptr->d=300; Cout<<dptr is derived type pointer\n; Dptr->show (); Cout<<using ((DC*) bptr)\n; ((DC*)bptr)->d=400; ((DC*)bptr)->show (); }

Virtual function:Prog. #includeiostream.h Class base { Public: Void display () {cout<<\ndisplay base;} Virtual void show () {cout<<\nshow base;} }; Class derived: public base { Public: Void display () {cout<<\ndisplay derived;} void show () {cout<<\nshow derived;} }; Void main () { Base B; derived D; base *bptr; Cout<<\nbptr points to base\n; Bptr=&b; Bptr->display (); Bptr->show (); Cout<<\n\nbptr points to base\n; Bptr=&D; Bptr->display (); Bptr->show (); }

Runtime polymorphism:Prog. #includeiostream.h #includestring.h Class media { Protected: Char title [50]; Float price; Public:

Media (char *s, float a) { Strcpy (title, s); Price=a; } Virtual void display () {} }; Class book: public media { Int pages; Public: Book (char *s, float a, int p): media(s, a) {pages=p;} Void display (); }; Class tape: public media {float time; Public:L Tape (char *s, float a, float t): media (s, a) {time=t;} Void display (); }; Void book::display () { Cout<<\ntitle:<<title; Cout<<\npages:<<pages; Cout<<\nprice:<<price; } Void tape::display () { Cout<<\ntitle:<<title; Cout<<\nplaytime:<<time<<mins; Cout<<\nprice:<<price; } Void main () { Char *title; Float price, time; Int pages; Cout<<\nENTER BOOK DETAILS\n; Cout<<title:;cin>>title; Cout<<price:;cin>>price; Cout<<pages:;cin>>pages; Book book1 (title, price, pages);

Cout<<\nENTER TAPE DETAILS\n; Cout<<title:;cin>>title; Cout<<price:;cin>>price; Cout<<playtime:;cin>>playtime; Book book1 (title, price, time); Media *list [2]; List [0]=& book1; List [1]=& tape1; Cout<<\nMEDIA DTAILS; Cout<<\n.BOOK.; List [0]->display(); Cout<<\n.TAPE.; List [1]->display(); }

Console input, output operators:Get () & put () operatorsProg. #includeiostream.h Void main () { Int count=0; Char c; Cout<<INPUT TEXT\n; Cin.get(c); While (c!=\n) { Cout.put (c); Count++; Cin.get (c); } Cout<<\nnumber of characters are<<count<<\n; }

Reading strings with getlineProg. #includeiostream.h Void main () {

Int size=20; Char city [20]; Cout<<enter city name:\n; Cin>>city; Cout<<city name:<<city<<\n\n; Cout<<enter city name again:\n; Cin. getline (city, size); Cout<<city name now:<<city<<\n\n; Cout<<enter another city name:\n; Cin.getline (city, size); Cout<<new city name:<<city<<\n\n; }

The write () operator:Prog. #includeiostream.h #includestring.h Void main () { Char *string1=C++; Char *string2=programming; Int m=strlen (string1); Int n=strlen (string2); For (int i=1;i<n;i++) { Cout.write(string2,i);cout<<\n; } For (i=n;i>0;i--) { Cout.write (string2,i);cout<<\n; } Cout.write(string1,m).write(string2,n); Cout<<\n; Cout.write(string1,10); }

ios format functions:Width() Precision() To specify the required field size for displaying an output value To specify the number of digits to be displayed after the decimal point of a float value To specify a character that is used to fill the unused portion of a field To specify format flags that can control the form of output display (such as left justification and right justification) To clear the flags specified

Fill() Setf()

Unsetf()

Manipulators:Manipulators
Setw() Setprecision() Sefill() Setiosflags() Resetiosflags() Width() Precision() Fill() Setf() Unsetf()

Equivalent ios functions

Specifying field size with width:Prog. #includeiostream.h Void main () { Int items [4]={10,8,12,15}; Int cost [4]={75,100,60,99}; Cout.width (5); Cout<<ITEMS; Cout.width (8); Cout<<COST; Cout.width(15); Cout<<TOTAL VALUE<<\n; Int sum=0; For (int i=0;i<4;i++)

{ Cout.width(5);cout<<items [i]; Cout.width(8);cout<<cost [i]; Int value=items [i] *cost[i]; Cout.width(15);cout<<value<<\n; Sum=sum+value; } Cout<<\ngrand total=; Cout.width(2);cout<<sum<<\n; }

Files:Real life problems handle values of data and in such situations we need to use some device such as floppy disk or hard disk to store the data. The data is stored in these devices using the concept of files.

Stream classes:Class
filebuf

Contents
Its purpose is to set the file buffers to read and write. Contains openprot constant used in the open () of file stream classes. Also contains close() and open () as members. Provides operations common to the file streams. Serves as a base for fstream, ifstream and ofstream classes. Contains open() and close() functions. Provides input operations. Contains open() with default input mode. Inherits the function get(), getline(), read(), seekg() and tellp() fuctions from istream. Provides output operations, contains open() with default output mode. Inherits put(), seekp(), tellp() and write() functions from ostream. Provides support for simultaneous input and output operations. Contains open() with default input mode. Inherits all the functions from istream and ostream classes through iostream.

fstreambase Ifstream Ofstream Fstream

Single file:Prog. #includeiostream.h Void main() { Ofstream outf (ITEM); Cout<<enter item name:; Char name[30]; Cin>>name; Outf<<name<<\n; Cout<<enter item cost:;

float cost; Cin>>cost; Outf<<cost<<\n; Outf.close(); Ifstream inf(ITEM); Inf>>name; Inf>>cost; Cout<<\n; Cout<<item name:<<name<<\n; Cout<<item cost:<<cost<<\n; Inf.close (); }

Multiple file:Prog. #includeiostream.h Void main() { Ofstream fout; Fout.open(country); Fout<<USA\n; Fout<<UK\n; Fout<<south korea\n; Fout.close(); Fout.open(capital); Fout<<Washington\n; Fout<<London\n; Fout<<Seoul\n; Fout.close(); Const int N=80; Char line[N]; Ifstream fin; Fin.open(country); Cout<<\ncontents of country file\n; While(fin) { Fin.getline(line,N); Cout<<line; } Fin.close(); Fin.open(capital); Cout<<\ncontents of capital file\n;

While(fin) { Fin.getline(line,N); Cout<<line; } Fin.close(); }

Reading two files simultaneously:Prog. #includeiostream.h #includestdlib.h Void main() { Const int SIZE=80; Char line[SIZE]; Ifstream fin1, ifn2; Fin1.open(country); Fin2.open(capital); For(int i=1;i<=10;i++) { If(fin1.eof()!=0) { Cout<<exit from country\n; Exit(1); } Fin1.getline(line, SIZE); Cout<<capital of<<line; if(fin2.eof()!=0) { Cout<<exit from capital\n; Exit(1); } Fin2.getline(line, SIZE); Cout<<line<<\n; } }

Input output operations:i/o operations on characters:-

Prog. #includefstream.h #includestring.h Void main() { Char string[80]; Cout<<enter a string\n; Cin>>string; Int len=strlen (string); Fstream file; File.open(TEXT, ios::in|ios::out); For(int i=0;i<len;i++) File.put(string[i]); File.seekg(0); Char ch; While (file) { file.seekg(0); char ch; } }

i/o operations on binary file:Prog. #includeiostream.h #includeiomanip.h void main() { float height4[]={175.5, 153.0, 167.25, 169.70}; ofstream outfile(filename); outfile.wriet((char*) &height, sizeof(height)); outfile.close(); for(int i=0;i<4;i++) height[i]=0; ifstream.h infile(filename); infile.read ((char*) &height, sizeof(height)); for(int i=0;i<4;i++) { cout.setf (ios::show point); cout<<setw(10)<<setprecision(2) <<height[i]; } infile.close(); }

Templates:(Syntax of program) template<class T> class vector { T*v; int size; public: vector(int m) { v=new T[size=m]; for(int i=0;i<size;i++) v[i]=0; } vector(T*a) { for(int i=0;i<size;i++) V[i]=a[i]; } T operator*(vector &y) { T sum=0; for (int i=0;i<size;i++) sum+=this->v[i]*y.V[i]; return sum; } }

S-ar putea să vă placă și