Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /// For virtual Function
- /// Code number 1:
- #include <iostream>
- using namespace std;
- class A{
- int a;
- public:
- A(){a = 0;}
- virtual void dis(){cout<<a<<endl;}
- };
- class B: public A{
- int b;
- public:
- B(){b = 2;}
- void dis(){cout<< b << endl;}
- };
- int main()
- {
- A *p;
- B x;
- p = &x;
- p->dis();
- }
- /// Code number 2
- /// Pure virtual Function
- #include <iostream>
- using namespace std;
- class A{
- int a;
- public:
- A(){a = 0;}
- virtual void dis()= 0;
- };
- class B: public A{
- int b;
- public:
- B(){b = 2;}
- void dis(){cout<< b << endl;}
- };
- int main()
- {
- A *p;
- B x;
- p = &x;
- p->dis();
- }
- ///Topix File read
- ///Code Number
- #include <iostream>
- #include <fstream>
- using namespace std;
- int main()
- {
- ifstream in("test.txt");
- char c;
- while(1)
- {
- in >> c;
- if(in.eof());
- break;
- cout<<c<<endl;
- }
- in.close();
- }
- /// Create a file and read
- /// Constructor for inheritance
- #include <iostream>
- using namespace std;
- class A
- {
- int x;
- public:
- A(int i){x = i;}
- void show() {cout<< x << endl;}
- };
- class B : public A{
- int y;
- public:
- B(int a , int l): A(l){y = a;}
- void dis(){cout<< y << endl;}
- };
- int main()
- {
- A p(3);
- p.show();
- }
- /// Generic Function (Template)
- Code 1
- #include <iostream>
- using namespace std;
- template <class t>
- t max1(t a , t b)
- {
- if(a > b)
- return a;
- else
- return b;
- }
- int main()
- {
- cout<< max1(1, 4) << endl;
- cout<< max1('c' , 'b')<< endl;
- cout<< max1(2.5 , 3.1)<< endl;
- }
- /// Generic class (template)
- #include <iostream>
- using namespace std;
- template <class t>
- class A
- {
- t a;
- public:
- A(){a = 0;}
- void set(t k)
- {
- a = k;
- }
- void dis(){cout<< a << endl;}
- };
- int main()
- {
- A <int> ob;
- ob.set(1);
- ob.dis();
- A <char> ob1;
- ob1.set('a');
- ob1.dis();
- }
- /// execption Handiling (Try , catch , Throw)
- #include <iostream>
- using namespace std;
- int main()
- {
- try{
- cout<< "try"<< endl;
- throw 1;
- }
- catch(int i)
- {
- cout<< "Catch " << i << endl;
- }
- }
- /// Virtual overrid (এই কোড টা ২ বার আসছে ) ট্রাইএঙ্গেল , রেক্ট্যেংল দিয়ে
- #include <iostream>
- using namespace std;
- class figure
- {
- public:
- double a , b;
- figure(double x , double y){a = x , b = y;}
- virtual double dis() = 0;
- };
- class rectangle: public figure
- {
- public:
- rectangle(double x, double y): figure(x , y){};
- double dis(){return a * b;}
- };
- class triangle: public figure
- {
- public:
- triangle(double x , double y): figure(x , y){};
- double dis(){return a * b;}
- };
- int main()
- {
- rectangle ob(2, 3);
- figure *a;
- a = &ob;
- cout<<a -> dis() << endl;
- triangle ob2(4 ,6);
- a = &ob2;
- cout<< a -> dis() << endl;
- }
Add Comment
Please, Sign In to add comment