Skip to main content

Posts

Showing posts from August, 2023

Swapping Private data of classes

  CASE 1:  Given two numbers a & b, swap these two numbers using the friend function of C++.  Examples:   Input : a = 5, b = 9 Output : a = 9, b = 5 Input : a = 4, b = 6 Output : a= 6, b = 4 Approach:  Create a class Swap, declare one variable in it, i.e., num, and create a constructor for inputs. Declare a friend function in it. Define the friend function outside the class scope by taking arguments as call by reference to pass the copy of Swap Object. Perform the swap operation.

Static Member Functions

  What is static member functions? A static member function can access static data members and static member functions inside or outside of the class. Static member functions have a scope inside the class and cannot access the current object pointer. You can also use a static member function to determine how many objects of the class have been created. Static Member Function in a class is the function that is declared as static because of which function attains certain properties as defined below: A static member function is independent of any object of the class.  A static member function can be called even if no objects of the class exist. A static member function can also be accessed using the class name through the scope resolution operator. A static member function can access static data members and static member functions inside or outside of the class. Static member functions have a scope inside the class and cannot access the current object pointer. You can also use a static me

Static Data Member

 A data member of a class can be qualified as statics. The properties of a static member variable are similar to that of a C static variable. A static member variable has certain special characteristics. These are: It is initialized to zero when the first object of its class is created. No other initialization is permitted. Only one copy of that member is created for the entire class and is shared by all the objectives of that class, no matter how many objects are created. It is visible only within the class, but its lifetime is the entire program. #include <iostream> using namespace std; class item {        static int count;        int number;        public:        void getdata(int a)        {            number=a;            count ++;        }        void getcount(void)        {            cout<< "count";            cout<< count <<"\n";        } }; int item::count; int main() {         item a,b,c;         a.getcount();         b.getcount();

Array of Objects

An array can be of any data type including struct. Similarly, we can also have arrays of variables of the type class. Such variables are called arrays of objects. Class Definition: class employee {           char name[30];          float age;     public:          void getdata(void);           void putdata(void); }; The identifier employee is a user-defined data type and can be used to create objects related to different employee categories. employee manage[3];          //aray of managers employee foreman[15];       //array of foreman employee worker[75];        // array of worker the array manager contains three objects(managers), namely, manager[0],  manager[1], and manager[2], of type employee class similarly, the foreman array contains 15 objects. and the worker array contains 75 objectives.(workers).