Visual C++: Click On All Programs Visual Studio Visual C++
Visual C++: Click On All Programs Visual Studio Visual C++
Visual Studio
Visual C++
File
OK
File New File(TAB) C++ source file Select ADD to Project Enter the file name and folder (same as project) to save OK
Experiment 1 //members_insideclass.cpp // Write a program that uses a class where the member functions are defined inside a class. #include<iostream.h> class add // class definition { private: int a,b; public: void input() //member functions are defined inside the class { cout<<"Enter the values of a and b\n"; cin>>a>>b; } void output() { cout<<"Result of addition is:\n"; cout<<a+b<<endl; } }; Continue to next Slide
Experiment 1 //members_insideclass.cpp void main() { add obj; //object declaration for class add obj.input(); obj.output(); }
Experiment 2 //classmembers_outside.cpp // Write a program that uses a class where the member functions are defined outside a class. #include<iostream.h> class add { private: int a,b; public: void input(); void output(); };
Continue to next Slide
Experiment 2 //classmembers_outside.cpp void add :: input() /*member functions are defined outside a class*/ { cout<<"Enter the values of a and b\n"; cin>>a>>b; } void add :: output() { cout<<"Result of addition is:\n"; cout<<a+b<<endl; }
Continue to next Slide
Experiment 2 //classmembers_outside.cpp void main() { add obj; //declaring object obj for class add obj.input(); obj.output();
Experiment 3 //static data members.cpp // Write a program to demonstrate the use of static data members. #include<iostream.h> class book { private: static int count; // static data member public: book() { count=1; } void addbook() { count++; } void displaybook() { cout<< "The Number Of The Books Are:"<< count<<endl; } }; Continue to next Slide
Experiment 3
//static data members.cpp
// Write a program to demonstrate the use of const data members. #include<iostream.h> class a { const int b; public: a(int c):b(c) // syntax for declaring constant data member { } void dis() { cout<<b<<endl; } }x(9);
Continue to next Slide
Experiment 5 //c_zero_parameterised.cpp
//Write a program to demonstrate the use of zero argument and parameterized constructor. #include<iostream.h> class numbers { private: int a,b; public: numbers() // zero argument { a=10; b=20; } numbers(int x,int y) // parameterized argument { a=x; b=y; } void display() { cout<<"a="<<a<<endl<<"b="<<b<<endl; } }; Continue to next Slide
Experiment 5 //c_zero_parameterised.cpp
void main() { numbers obj,obj1(30,40); obj.display(); obj1.display(); }
Experiment 6 //copy_constructor.cpp /*WAP to show the example of dynamic constructor (copy constructor ). WAP which reads a complex number and copies to another number using copy constructor*/ # include<iostream.h> class complex { int r,i; public: complex( int a, int b) { r = a ; i = b; }
complex( complex &c) { r = c.r ; i = c.i; } void show() { cout<< r <<" + i" << i <<endl; } };
Continue to next Slide
Experiment 6 //copy_constructor.cpp void main() { int x ,y; cout<<"Enter the Real Part :" ; cin >> x; cout<<"Enter the Imag Part :" ; cin>>y; complex c1(x,y); // 1st constructor is called complex c2(c1); // copy constructor is called cout<<"first Number is :"; c1.show(); cout<<"Second Number is :"; c2.show(); }
Experiment 7 //explict_constructor.cpp // WAP to show the example of explicit constructor. # include<iostream.h> class number { int m,n; public: number( int, int); //constructor declared void display( void) { cout<<"m = "<<m<<endl; cout<<"n = "<<n<<endl; } }; number::number(int x, int y) //constructor defined { m = x; n = y; } Continue to next Slide
Experiment 7 //explict_constructor.cpp
void main() { number n1 = number(56,78); /*constructor called explicitly */ cout<<"The number are: "<<endl; n1.display(); }
Experiment 8 //overloading_inc_dec_operator.cpp
//Write a program to demonstrate the overloading of increment and decrement operators. #include<iostream.h> class incdec { private: int a,b; public: incdec() { a=5; b=8; } void operator ++() { a++; b++; } void operator --() { a--; b--; } void out() { cout<<"a="<<a<<endl; cout<<"b="<<b<<endl; cout<<endl; } }; Continue to next Slide
Experiment 8 //overloading_inc_dec_operator.cpp
Experiment 9 //overlo_binary.cpp
// WAP to show the example of operator overloading (unary operator) #include<iostream.h> class complex { int r,i; public: void getdata() { cout<<"Enter the Real part : "; cin>>r; cout<<"Enter the Imag part : "; cin>>i; } void operator ++() //overload ++ { ++r; ++i; } void showdata() { cout<< r << " + i " << i <<endl; } }; Continue to next Slide
Experiment 9 //overlo_binary.cpp
void main() { complex c1; c1.getdata(); cout << "Number before increament "<<endl; c1.showdata(); c1++; cout << "Number after increament "<<endl; c1.showdata(); }
Experiment 10 //typecasting_basic.cpp
//Write a program to demonstrate the typecasting of basic type to class type. #include<iostream.h> class distance { int feet; float inches; public: distance(float mtr) { float f=3.28*mtr; feet=int(f); inches=12*(f-feet); } void show() { cout<<"distance in feet and inches is : "<<endl; cout<<feet<<"\'-"<<inches<<"\""<<endl; } }; Continue to next Slide
Experiment 10 //typecasting_basic.cpp
void main() { float meter; cout<<"enter a distance in meters:"; cin>>meter; distance d1=meter; d1.show(); }
Experiment 11 //typecasing_class_basic.cpp
//Write a program to demonstrate the typecasting of class to basic type. #include<iostream.h> class distance { int feet; float inches; public: distance(int f,float i) { feet=f; inches=i; } void show() { cout<<feet<<"\'-"<<inches<<"\""<<endl; } operator float () { float ft=inches/12; ft=ft+feet; return(ft/3.28); } }; Continue to next Slide
Experiment 11 //typecasing_class_basic.cpp
void main() { distance d1(3,3.36); float m=d1; cout<<"distance in meter:"<<m<<endl; cout<<"distance in feet and inches:"<<endl; d1.show(); }
Experiment 12 //typecasting_class_class.cpp
//Write a program to demonstrate the typecasting of class type to class type. #include<iostream.h> #include<math.h> class polar { float rd; float ang; public: polar() { rd=ang=0.0; } polar(float r,float a) { rd=r; ang=a; } float getrd() { return(rd); } float getang() { return(ang); } void show() { cout<<rd<<","<<ang<<endl; } }; Continue to next Slide
Experiment 12 //typecasting_class_class.cpp
class rec { float x; float y; public: rec() { x=y=0.0; } rec(float xco,float yco) { x=xco; y=yco; } rec(polar p) { float r=p.getrd(); float a=p.getang(); x=r*cos(a); y=r*sin(a); } void showrec() { cout<<x<<","<<y<<endl; } }; Continue to next Slide
Experiment 12 //typecasting_class_class.cpp
void main() { rec r1; polar p1(2.0,90.0); r1=p1; cout<<"polar co."<<endl; p1.show(); cout<<"rec co."<<endl ; r1.showrec(); }
//object declaration for 'DERIVE1' //member function of 'BASE" //member function of 'DERIVE" //member function of 'DERIVE1"
Experiment 15 //virtual_derivation_class.cpp
//Write a program to demonstrate the virtual derivation of a class. #include<iostream.h> class base // virtual class { protected: int data; public: base() { data=10; } };
Continue to next Slide
Experiment 15 //virtual_derivation_class.cpp
class d1:virtual public base // 'D1' derived from 'BASE' { }; class d2:virtual public base // 'D2' derived from 'BASE' { }; class c:public d1,public d2 // 'c' derived from 'D1' and 'D2' { public: int showdata() { return(data); } }; Continue to next Slide
Experiment 15 //virtual_derivation_class.cpp
void main() { c obj ; // declaring object for 'c' int k; k=obj.showdata(); cout<<"k="<<k<<endl; }
Experiment 17 //exception_handling.cpp
//Write a program to demonstrate the exception handling. #include<iostream.h> void main() { int f,s; cout<<"Enter the first no."; cin>>f; cout<<"Enter the second no.( any number to check division and 0 to show exception)"; cin>>s; try { if(s!=0) { cout<<"division="<<f/s<<endl; } else { throw(s); /* 'throw' and 'catch' are keywords for error handling*/ } } catch(int n) { cout<<"There is an exception division by "<<n<<endl; } } Continue to next Slide
Experiment 18 // function_template.cpp
// Write a program to demonstrate the use of function template. #include<iostream.h> template<class T> T max(T a,T b) // declaring member function 'max' for template 'T' { return (a>b?a:b); /* returns the greater no: if a > b then returns a else b */ } void main() { cout<<max(10,15)<<endl; cout<<max('K','s')<<endl; cout<<max(10.1,20.2)<<endl; }
//Write a program to demonstrate the use of class template. #include<iostream.h> template<class T> class A { T a,b; public: A() {
Experiment 19 //class_template.cpp
a=b=0; } A(T x,T y) // parameterised constructor { a=x; b=y; } void display() { cout<<a<<endl<<b<<endl; } T add() { return(a+b); } }; Continue to next Slide
Experiment 20 //class_template.cpp
void main() { A<int>a1(10,20); /* declaring object for 'A' to return int sum of two numbers*/ A<float>a2(10.5,19.7); /* declaring object for 'A' to return float sum of two numbers*/ int r1; float r2; r1=a1.add(); r2=a2.add(); a1.display(); // show int sum a2.display(); // show float sum cout<<endl<<r1<<endl<<r2; }