Posts

Showing posts from March, 2023

program for Object Composition and Delegation

Image
Write a program in program notebook  #include<iostream.h> #include<conio.h> class fclass { public: int data; fclass() // here is constructor declared { data = 0; // Data member is initialized } fclass(int a) // here is constructor  again declared { cout << "Constructor of Class fclass is calling " << endl; data = a;   // Data member is initialized } }; class sclass { private: int b; fclass fobject;   //here fobject is object  for fclass public: sclass(int a) : fobject(a)  // Constructor of class sclass { b = a;     // initialized } void pdata() // here pdata function is used to print value { cout<<"Data of class SClass object is: "<<b<<endl; cout<<"Data of class AClass object in class SClass is:"<<fobject.data; } }; int main() { sclass s(99); // Here s is created for  sclass s.pdata();    //calling pdata member function of seconed class getch(); return 0; } Output:

object composition and delegation (examples)

Image
Write after object composition   For Example : Write a program to demonstrate the use object composition. #include<iostream.h> #include<conio.h> class first { private:   int a; public: void setValue(int b) { a = b; }   void getSum(int c) { cout<<" Addition of   "<<a<<" and "<<c<<" = "<<(a + c)<<endl; } };   class second { public: first x; //here x is object of first class and it is composition void showResult() { x.getSum(10); } };   void main() { second s; s.x.setValue(10); s.x.getSum(90); s.showResult(); getch(); } Output : Write after object delegation   For Example : Write a program to demonstrate the use object delegation #include<iostream.h> #include<conio.h> class first { public:   void display() { cout << "The First  Class is calling"; } };   class second { first f;  // Here object of first cl

Member function overloading

For Example: Write a program demonstrate the use member function overloading. #include<iostream.h> #include<conio.h>   void sunfun(int a, int b)   //Function declaration  and definition  in a single section  { cout<<"Addition of Two Integer Number = "<<(a + b)<<endl; }   void sunfun(double a, double b) //again sunfun() is defined  { cout<<"Addition of Two Double Number = "<<(a + b); } int main() { sunfun(22,13); //function calling  sunfun(9.2,7.4); //function calling  getch(); return 0; }  

Pointer to the derived class and base class

Image
  For Example: Write a program to declare a pointer to the derived class and access the member variable of base and derived class. #include<iostream.h> #include<conio.h> class A { public: int b; void display() { cout<<"Value B is="<<b<<endl; } };   class B : public A { public: int d; void display() { cout<<"Value of B is ="<<b<<endl<<"Value of D is ="<<d<<endl; } };   void main() { B *cp; // here cp is pointer object derived class B b;    // here b is object of B class cp=&b;   //initialized simple object to pointer object cp->b=200; //Assign value to pointer object cout<<"CP points to the derived object"<<endl; cp->display(); //member function calling with the help of pointer object getch(); } Output:

Behavior of constructors and destructors in inheritance

Image
  For Example: Write a program to demonstrate the use of  constructors and destructors   in inheritance . #include<iostream.h> #include<conio.h> class base       //base class { public: base()          //constructor { cout<<"Base class constructor"<<endl; }   ~base()         //destructor { cout<<"Base class Desctructor"<<endl; } };   class derived : public base            //Derived   class/ child class { public: derived()                                                                                             //constructor of derived class { cout<<"Derived class constructor"<<endl; }   ~ derived()                                                                         //destructor of derived class { cout<<"Derived class destructor"<<endl; } };   int   main() { derived d; //Automatically executes both derived class and b

Virtual Base Classes..

Image
  Virtual Base Classes:  Program: Write a program to demonstrate the use of Virtual Base Class #include<iostream.h> #include<conio.h> class student { protected: int rollno; public: void get_number(int a) { rollno = a; }   void put_number(void) { cout<<"Roll No:"<<rollno<<endl; } };   class test:virtual public student { protected: float part1,part2; public: void get_marks(float x, float y) { part1=x; part2=y; }   void put_marks(void) { cout<<"Marks Obtained:"<<"\n"     <<"Part1="<<part1<<"\n"                  <<"Part2="<<part2<<"\n";                  }     };   class sports:public virtual student { protected: float score; public: void get_score(float s) { score=s; } void put_score(void) { cout<<"Sports Score:"<<score<<endl; } };   class result:public test, public sports { float total; public: void display(void); }