15
MANIPULATORS: Endl Manipulator: #include<stdio.h> #include<conio.h> void main() { cout<<”HELLO”<<endl; cout<<”BCA”<<endl; } Output: HELLO BCA Setw Manipulators: #include<stdio.h> #include<iomanip.h> void main() { int x1= 1,x2=2,x3=3; cout<<setw(8)<<”Hello”<<setw(20) <<”bca”<<endl; <<setw(8)<<”Read”<<setw(20)<<x1< <endl; <<setw(8)<<”Well”<<setw(20)<<x2< <endl; << setw(8)<<”Girls”<<setw(20)<<x3<< endl; } OUTPUT: Hello bca Read 1 Well 2 Girls 3 Setfill manipulators: #include<stdio.h> #include<iomanip.h> void main() { cout<<setw(10)<<setfill(‘@’)<<”5 ”<<”3”<<endl; } OUTPUT: @@@@@@@@53 Setprecision manipulators: #include<stdio.h> #include<iomanip.h> void main() { float x=0.1; cout<<fixed<<setprecision(3)<<x< <endl; cout<<scientific<<setprecision(1 5)<<x<<endl; } OUTPUT: 0.100 0.100E-001 INHERITANCE: SINGLE INHERITANCE: class base { char name[20]; int rollno;cChar gender; Public: void getdata(); void display(); }; class derived:public base { float height,weight; public: void getfit(); void displayfit(); }; void base::getdata() { cout<<”Enter the name of a student:”<<endl; cin>>name; cout<<”Enter the roll no of a student:”<<endl; cin>>rollno; 1

bca2 c++ programs

  • Upload
    deepi

  • View
    4

  • Download
    0

Embed Size (px)

DESCRIPTION

programs

Citation preview

Page 1: bca2 c++ programs

MANIPULATORS:Endl Manipulator:#include<stdio.h>#include<conio.h>void main(){cout<<”HELLO”<<endl;cout<<”BCA”<<endl;}Output:HELLOBCASetw Manipulators:#include<stdio.h>#include<iomanip.h>void main(){int x1= 1,x2=2,x3=3;cout<<setw(8)<<”Hello”<<setw(20)<<”bca”<<endl;<<setw(8)<<”Read”<<setw(20)<<x1<<endl;<<setw(8)<<”Well”<<setw(20)<<x2<<endl;<< setw(8)<<”Girls”<<setw(20)<<x3<<endl;}OUTPUT:Hello bcaRead 1Well 2Girls 3Setfill manipulators:#include<stdio.h>#include<iomanip.h>void main(){cout<<setw(10)<<setfill(‘@’)<<”5”<<”3”<<endl;}OUTPUT:@@@@@@@@53Setprecision manipulators: #include<stdio.h>#include<iomanip.h>void main(){float x=0.1;cout<<fixed<<setprecision(3)<<x<<endl;cout<<scientific<<setprecision(15)<<x<<endl;}

OUTPUT:0.1000.100E-001

INHERITANCE:SINGLE INHERITANCE:

class base{char name[20];int rollno;cChar gender;Public:void getdata();void display();};class derived:public base{float height,weight;public:void getfit();void displayfit();};void base::getdata(){cout<<”Enter the name of a student:”<<endl;cin>>name;cout<<”Enter the roll no of a student:”<<endl;cin>>rollno;cout<<”Enter the gender of a student:”<<endl;cin>>gender;}void base::display(){cout<<”Name=”<<name;cout<<”Rollno=”<<rollno;cout<<”Gender=”<<gender;}void derived::getfit(){cout<<”Enter the height & weight of a student:”;cin>>height>>weight;}void derived ::displayfit(){cout<<”Height=”<<height;cout<<”weight=”<<weight;}

1

Page 2: bca2 c++ programs

int main(){derived d;d.getdata();d.display();d.getfit();d.displayfit();return ();}OUTPUT:Enter the name of a student: SanaEnter the roll no of a student: 101Enter the gender of a student: femaleName = SanaRoll no=101Gender= femaleEnter the height & weight of a student:5.6 42Height = 5.6Weight = 42

MULTIPLE INHERITANCE:

class mprotected:int m;public:void get_m(int x);};class n{protected :int n;public:void get_n(int y);};class p:public m,public n{public:void display();};void m::get_m ( int x){m = x;}void n:: get_n ( int y){n=y;}

void p::display(){cout<<”M=”<<m;cout<<”N=”<<n;cout<<”M*N=”<<m * n;}int main(){p s;s.get_m(10);s.get_n(20);s.display();getch();return o;}OUTPUT:M = 10N =20 M* N = 200

MULTI LEVEL INHERITANCE:

class student{protected :int rollno;public:void getno();void putno();};class test:public student{protected:int m1,m2,m3,m4,m5,m6;public:void getmarks();void putmarks();};class result:public test{int total;public:void display();};void student::getno(){cout<<”Enter the roll no of a student:”;cin>>rollno;}

2

Page 3: bca2 c++ programs

void student :: putno(){cout<<”Rollno=”<<rollno;}void test :: getmarks(){cout<<”Enter six subject marks one by one:”;cin>>m1>>m2>>m3>>m4>>m5>>m6;}void test ::putmarks(){cout<<”Marks are:”<<m1<<m2<<m3<<m4<<m5<<m6;}void result::display(){Total=m1+m2+m3+m4+m5+m6;cout<<”Total =”<<total;}int main(){result s1;s1.getno();s1.getmarks();s1.putno();s1.putmarks();s1.display();return 0 ;}OUTPUT:Enter the roll no of a student: 101Enter six subject marks one by one: 808789909067Rollno= 101Marks are : : 808789909067Total = 503

PASS BY VALUE:

#include<iostream.h>void swap( int , int);void main(){int var1,var2;cout<<”Enter two numbers:”<<endl;cin>>var1>>var2;cout<<”In Main :”<<var1<<var2<<endl;swap(var1,var2);

}void swap ( int num1,int num2){int temp;temp=num1;num1=num2;num2=temp;cout<<”In Swap:”<<num1<<num2<<endl;}OUTPUT:Enter two numbers: 2 4In Main: 2 4In Swap: 4 2

PASS BY REFERENCE:

#include<iostream.h>void main(){int number=5;int & ref= number;cout<<”Number is :”<<number<<endl;cout<<”Increasing the number ….”<<endl;number++;cout<<”Now number is : “<<number <<endl;ref++;cout<<”Now reference is :”<<ref<<endl;cout<<”Now number is :”<<number<<endl;}OUTPUT:Number is : 5Increasing the number ….Now number is : 6Now reference is : 7Now number is : 7

3

Page 4: bca2 c++ programs

POINTER:#include<iostream.h>int main(){int * x;int c=200;int p;x=&c;p=*x;cout<<”The Address of memory location of x:”<<x<<endl;cout<<”The content of the pointer x:”<<*x<<endl;cout<<”The content of the variable p: “<<p <<endl;return 0;}OUTPUT:The Address of memory location of x: 0012fThe content of the pointer x: 200The content of the variable p: 200

POINTERS & ARRAYS:

#include<iostream.h>int main(){int age[5];int * p;int sum=0,i;p = age;for(i=0;i<5;i++){cout<<”Enter the age of a student:”<<endl;cin>>*p;sum=sum+*p;P++;}p=age;cout<<”sum of ages :”<<sum<<endl;cout<<”The age of the last student is :”<<*(p+4)<<endl;return 0;}OUTPUT:Enter the age of a student: 12Enter the age of a student: 11Enter the age of a student: 15

Enter the age of a student: 17Enter the age of a student: 18sum of ages: 73The age of the last student is : 18

CONSTRUCTOR & DESTRUCTOR:

#include<iostream.h>class simple{private:int a,b;public:simple(){a=0;b=0;cout<<”\n Constructor of class simple”;}~ simple(){cout<<”\n Destructor of class simple”;}void getdata(){cout<<”\n Enter values for a & b:”;cin>>a>>b;}void putdata(){cout<<”\n The two integers :”<<a<<b;cout<<”\n The sum of the variables :”;<<a+b;}};void main(){simple s;s.getdata();s.putdata();}OUTPUT:Constructor of class simpleEnter values for a & b: 2 4The two integers : 2 4The sum of the variables :6Destructor of class simple

4

Page 5: bca2 c++ programs

DEFAULT CONSTRUCTOR:#include<iostream.h>class integer{Private:int m,n;public:integer ();void display();};integer :: integer(){m=0;n= 0;}void integer :: display(){cout<<”M value is = “<<m <<endl;cout<<”N value is = “<<n <<endl;}int main(){cout<<”Demonstration of Default constructor\n”;integer dc;dc.display();return 0;}OUTPUT:Demonstration of Default constructorM value is = 0N value is = 0

PARAMETERISED CONSTRUCTOR:

#include<iostream.h>class integer{int m,n;public:integer ( int x, int y);void display();};integer :: integer (int x, int y){m=x;n=y;}

void integer ::display(){cout<<”M value is = “<<m<<endl;cout<<”N value is = “<<n<<endl;}int main(){integer pc(20,50);cout<<”Demonstration of parameterized constructor\n”;pc.display();return 0;}OUTPUT:Demonstration of parameterized constructor M value is = 20N value is = 50

COPY CONSTRUCTOR:#include<iostream.h>class example{int a,b;public:example ( int x, int y){a=x;b=y;cout<<”\n Im constructor”;}void display(){cout<<”\n Values :”<<a<<”\t”<<b;}};int main(){example object1(10,20);example object2=object1;object1.display();object2.display();return 0;}OUTPUT:Im constructorValues : 10 20Values : 10 20

5

Page 6: bca2 c++ programs

DYNAMIC CONSTRUCTOR:

#include <iostream.h>#include <conio.h>class Account{private:int account_no;int balance;public :Account(int a,int b){account_no=a;balance=b;}void display(){cout<< "\nAccount number is : "<< account_no;cout<< "\nBalance is : " << balance;}};void main(){clrscr();int an,bal;cout<< "Enter account no : ";cin >> an;cout<< "\nEnter balance : ";cin >> bal;Account *acc=new Account(an,bal); //dynamic constructoracc->display(); //'->' operator is used to access the methodgetch();}

OUTPUT:

Enter account no : 121Enter balance: 1000Account no is: 121Balance is : 1000

6

Page 7: bca2 c++ programs

OPERATOR OVERLOADING

#include <iostream>class temp{ private: int count; public: temp():count(5)

void operator ++() { count=count+1; } void Display() { cout<<"Count: "<<count; }};int main(){ temp t; ++t; /* operator function void operator ++() is called */ t.Display(); return 0;}}Output

Count: 6

INLINE FUNCTION:

#include <iostream> int fun(int); void main( ) {         int x;         cout << "n Enter the Input Value: ";         cin>>x;         cout << "n The Output is: " << fun(x); }

  inline int fun(int x1) {         return 5*x1; }OUTPUT:Enter the Input Value: 5The Output is : 25

FRIEND FUNCTION:1. #include <iostream>2. class fun3. {4. private:5.         int a,b;6. public:7.         void test()8.         {9.                 a=100;10.                b=200;11.        }12.        friend int compute(fun

e1);13. };14. int compute(fun e1)15.{16.                return int(e1.a+e1.b)-

5;17.}18. void main()19.{20.        fun e;21.        e.test();22.        cout << "The result is:" <<

compute(e);23.                }

OUTPUT:The result is : 295

7

Page 8: bca2 c++ programs

ANOTHER PROGRAM FOR FRIEND FUNCTION:#include<iostream.h>#include<conio.h>class  base{    int val1,val2;   public:    void get()    {       cout<<"Enter two values:";       cin>>val1>>val2;    }    friend float mean(base ob);};float mean(base ob){   return float(ob.val1+ob.val2)/2;}void main(){    clrscr();    base obj;    obj.get();    cout<<"\n Mean value is : "<<mean(obj);

    getch();}          Output:Enter two values: 10, 20

Mean Value is: 15

COMMAND LINE ARGUMENTS:

#include <stdio.h>#include <stdlib.h> int main(int argc, char *argv[]) // command line arguments{if(argc!=5) { printf("Arguments passed through command line " \ "not equal to 5"); return 1;} printf("\n Program name : %s \n", argv[0]); printf("1st arg : %s \n", argv[1]); printf("2nd arg : %s \n", argv[2]); printf("3rd arg : %s \n", argv[3]); printf("4th arg : %s \n", argv[4]); printf("5th arg : %s \n", argv[5]); return 0;}

Output:

Program name : test1st arg : this2nd arg : is3rd arg : a4th arg : program5th arg : (null)

VIRTUAL FUNCTION:

8

Page 9: bca2 c++ programs

#include <iostream>class B{ public: void display() { cout<<"Content of base class.\n"; }};class D : public B{ public: void display() { cout<<"Content of derived class.\n"; }};int main(){ B *b; D d; b->display(); b = &d; /* Address of object d in pointer variable */ b->display(); return 0;}

Output

Content of base class.Content of base class.

ANOTHER PROGRAM FOR VIRTUAL FUNCTION:

#include <iostream>class B{ public: virtual void display() /* Virtual function */ { cout<<"Content of base class.\n"; }};class D1 : public B{ public: void display() { cout<<"Content of first derived class.\n"; }};class D2 : public B{ public: void display() { cout<<"Content of second derived class.\n"; }};int main(){ B *b; D1 d1; D2 d2;b = &d1; b->display(); /* calls display() of class derived D1 */ b = &d2; b->display(); /* calls display() of class derived D2 */}OutputContent of first derived class.Content of second derived class.

9

Page 10: bca2 c++ programs

C++ Basic Input/OutputThe standard output stream (cout):#include <iostream> int main( ){ char str[] = "Hello C++"; cout << "Value of str is : " << str << endl;}OUTPUT:Value of str is : Hello C++The standard input stream (cin):#include <iostream>int main( ){ char name[50]; cout << "Please enter your name: "; cin >> name; cout << "Your name is: " << name << endl; }OUTPUT:Please enter your name: SanaYour name is: SanaThe standard error stream (cerr):#include <iostream>int main( ){ char str[] = "Unable to read...."; cerr << "Error message : " << str << endl;}OUTPUT:Error message : Unable to read....

The standard log stream (clog):#include <iostream>int main( ){

char str[] = "Unable to read...."; clog << "Error message : " << str << endl;}OUTPUT:Error message : Unable to read....

TYPE CASTING OR TYPE CONVERSIONAutomatic Conversion otherwise called as Implicit Conversion:

#include <iostream>void main(){short x=6000;int y;y=x;}

Type casting otherwise called as Explicit Conversion:

#include <iostream.h>void main(){int a;float b,c;cout << "Enter the value of a:";cin >> a;cout << "Enter the value of b:";cin >> b;c = float(a)+b;cout << "The value of c is:"<<c;}

OUTPUT:Enter the value of a:10Enter the value of b:12.5The value of c is :22.5

10

Page 11: bca2 c++ programs

ERROR HANDLING DURING FILE OPERATION:-----if stream infile;infile.open(“ABC”);while(!infile.fail()){----- process the file-------}if(infile.eof()){----- terminate program normally}elseif(inline.bad()){--- report error}else{infile clear(); \\ clear error state}----------clear();while(infile){-----}andwhile(infile.read(…)){----}

11

Page 12: bca2 c++ programs

INDEX FOR C++ PROGRAMS

S.NO

TITLE PAGE1. MANIPULATORS:

ENDL,SETW,SETFILL,SETPRECISION. 12. INHERITANCE:

SINGLEMULTIPLEMULTI LEVEL

122

3. PASS BY VALUEPASS BY REFERENCE

33

4. POINTER 45. POINTER & ARRAY 46. CONSTRUCTOR & DESTRUCTOR 47. TYPES OF CONSTRUCTOR:

DEFAULT CONSTRUCTORPARAMETERISED CONSTRUCTORCOPY CONSTRUCTORDYNAMIC CONSTRUCTOR

5556

8. OPERATOR OVERLOADING 79. INLINE FUNCTION 710. FRIEND FUNCTION 711. COMMAND LINE ARGUMENTS 812. VIRTYAL FUNCTION 913. C++ BASIC INPUT /OUTPUT 1014. TYPE CASTING OR TYPE CONVERSION 1015. ERROR HANDLING DURING FILE OPERATION 11

12

Page 13: bca2 c++ programs

13