Saturday, April 19, 2025

C++ OO Protected vs Private

Protected and private are two access modifiers used to restrict the access of class members.

The private access modifier restricts the access to the class members only within the same class. This means that only the class itself can access its own private members, and no other code outside the class can access or modify them. This is often used to keep the implementation details of a class hidden from other parts of the code.

The protected access modifier is similar to private, but it allows the members to be accessed within the class itself and any subclasses that inherit from the class. This means that the protected members can be accessed by the subclass and its instances, but not by any other code outside the class hierarchy. 


#include <iostream>

using namespace std;

class Prim{
    protected:
        int height;

    public:
        string name;
        int age;

        void setName(string x) {
            name = x;
        }

        void setAge(int y) {
            age = y;
        }
};

class Sec : public Prim {
    public:
        string job;

        void setJob(string z) {
            job = z;
        }

        void setHeight(int h) {
            height = h;
        }

        void printAll() {
            cout << "Name: " << name << endl;
            cout << "Age: "  << age  << endl;
            cout << "Job: "  << job  << endl;
            cout << "Height: " << height << endl;
        }
};

int main() {

    Sec Object;
    Object.setName("Michael");
    Object.setAge(30);
    Object.setJob("Canceler");
    Object.setHeight(184);

    Object.printAll();

    return 0;
}

This code example demonstrate the usage of protected access specifier in inheritance.

In this code, there are two classes, Prim and Sec.

Sec is inherited from Prim using the public access specifier. This means that all the public members of the base class will become public in the derived class, and all the protected members of the base class will become protected in the derived class.

The Prim class has a protected integer variable named height, which is not accessible outside of the class but can be accessed within the class and its derived classes.

The Sec class has a public string variable job and three member functions - setJob(), setHeight(), and printAll(). The setJob() function sets the job of the object, the setHeight() function sets the height of the object, and the printAll() function prints all the details of the object.

The printAll() function also accesses the height variable, which is a protected member of the Prim class. This is possible because Sec is a derived class of Prim, and height is a protected member of Prim, so it can be accessed within the derived class.

In the main() function, an object of the Sec class is created, and its various member functions are called to set its attributes and display its details.

C++ OO Inheritance

Inheritance is a mechanism that allows a class (known as a derived class or subclass) to inherit the properties (data members and member functions) of another class (known as the base class or superclass).

When a derived class inherits from a base class, it automatically gets access to all the public and protected members of the base class. 

Inheritance is a key feature of object-oriented programming and allows for code reuse and the creation of hierarchies of classes that share common properties. 


#include <iostream>

using namespace std;

class Prim{
    public:
        string name;
        int age;

        void setName(string x) {
            name = x;
        }

        void setAge(int y) {
            age = y;
        }
};

class Sec : public Prim {
    public:
        string job;

        void setJob(string z) {
            job = z;
        }

        void printAll() {
            cout << "Name: " << name << endl;
            cout << "Age: "  << age  << endl;
            cout << "Job: "  << job  << endl;
        }
};

int main() {

    Sec Object;
    Object.setName("Michael");
    Object.setAge(30);
    Object.setJob("Canceler");

    Object.printAll();

    return 0;
}

This C++ code defines two classes Prim and Sec where Sec is derived from Prim using inheritance.

The Prim class has two member variables, name and age and two member functions, setName() and setAge(), which are used to set the values of name and age, respectively.

The Sec class also has a member variable job and two member functions, setJob() and printAll().

The setJob() function is used to set the value of the job member variable. The printAll() function is used to print all the member variables of both Prim and Sec classes. In this function, the name and age member variables are inherited from the Prim class, whereas the job member variable is defined in the Sec class.

In the main() function, an object of Sec class is created, named Object. Then the setName(), setAge() and setJob() member functions are called to set the values of name, age and job, respectively.

Finally, the printAll() function is called to print all the member variables of the object Object.

C++ OO Friend Functions

A friend function is a non-member function that is granted access to the private and protected members of a class. This means that a friend function can access private and protected data members of a class as if it were a member of that class.

Friend functions are declared inside the class definition but are defined outside the class definition. They are declared using the friend keyword followed by the function signature, just like any other function declaration.

Friend functions are useful when you need to access the private or protected members of a class from outside the class definition, but you don't want to make those members public. 


#include <iostream>

using namespace std;

class Cls{
    public:
        string name = "Michael";
        int age = 40;

        friend void friendFunction(Cls x);
};

void friendFunction(Cls x) {
    cout << x.name << " " << x.age << endl;
}

int main() {

    Cls Object;
    friendFunction(Object);

    return 0;
}

This C++ code defines a class Cls with two public data members name and age. It also declares a friend function friendFunction() that takes an object of Cls as its argument.

The friendFunction() function is defined outside the class definition but is declared as a friend of the Cls class using the friend keyword. This means that the friendFunction() function can access the private and protected members of the Cls class.

In the main() function, we create an object of Cls named Object, and then pass it as an argument to the friendFunction() function. The friendFunction() function simply prints out the name and age data members of the Cls object passed to it.

What's the main difference between "private" and "protected"

Private and protected are access specifiers that are used to define the visibility of class members.

private members are only accessible from within the class where they are declared. They cannot be accessed from outside the class or by any derived classes. This means that they are hidden from the outside world, and can only be accessed through public member functions (methods) of the class.

protected members are also hidden from the outside world, but can be accessed by derived classes. This means that they are accessible from the class in which they are declared and any classes derived from that class. However, they cannot be accessed by any other classes or functions outside of the class hierarchy.

The main difference between private and protected members is that private members are completely hidden from the outside world, while protected members are accessible to derived classes. This allows derived classes to access and modify the protected members of their base class, but not the private members.

In general, it is good practice to use the private access specifier for most members, and only use protected when it is necessary to provide access to derived classes. This helps to ensure that the internal implementation of the class is not exposed to the outside world, and that the class can maintain its invariants and encapsulation.

C++ OO Static Method in Classes

Static methods are member functions of a class that can be called without creating an instance of the class. They can be accessed using the class name followed by the scope resolution operator ::, without needing to create an object of the class.

Static methods are defined using the static keyword in the class definition and do not have access to non-static member variables or functions of the class. They can only access static member variables and other static member functions. 

Static methods can be used to perform operations that do not depend on the state of an object of the class and can be called without needing to create an instance of the class.


#include <iostream>

using namespace std;

class Cls{
    public:
        static int counter;

        Cls() {
            cout << "Value of Counter Atm: " << ++counter << endl;
        }

        void normalMethod() {
            cout << "Value of Counter Atm: " << ++counter << endl;
        }

        static void staticMethod() {
            cout << "Value of Counter Atm: " << ++counter << endl;
        }
};

int Cls :: counter = 0;

int main() {

    Cls :: staticMethod();
    Cls :: staticMethod();
    Cls :: staticMethod();
    Cls :: staticMethod();

    Cls Obj1;
    Obj1.normalMethod();

    Cls :: staticMethod();

    return 0;
}

This C++ code defines a class named Cls with a static integer variable counter. The class has a constructor that increments the counter variable each time an object of the class is created, and prints the current value of counter to the console. It also has a non-static member function normalMethod that increments the counter variable and prints its current value. In addition, it has a static member function staticMethod that increments the counter variable and prints its current value.

The counter variable is declared as static within the Cls class, so it is shared by all instances of the class. This means that each time the counter variable is incremented, its new value is retained for the next call.

In the main function, the staticMethod of the Cls class is called four times using the class name followed by the scope resolution operator ::. Each call increments the counter variable and prints its current value.

After that, an object of the Cls class named Obj1 is created, which increments the counter variable in its constructor and prints its current value using the normalMethod.

Finally, staticMethod is called again using the class name followed by the scope resolution operator ::, which increments the counter variable and prints its current value.

C++ OO Static Variables in Classes

A static variable is a variable that is declared with the keyword static within a function or class. A static variable exists for the lifetime of the program, and its value is retained between function calls or object instances. 


#include <iostream>

using namespace std;

void functionSt();

int main() {

    functionSt();
    functionSt();
    functionSt();
    functionSt();

    return 0;
}

void functionSt() {
    static int counter = 0;
    cout << "Value Atm: " << ++ counter << endl;
}

This code defines a function named functionSt() that increments a static variable named counter each time it is called, and prints the value of the counter to the console. The main function calls the functionSt() function four times.

When the functionSt() function is called for the first time, the counter variable is initialized to 0. The ++ counter expression increments the counter by 1, and the new value of the counter is printed to the console. On subsequent calls to functionSt(), the counter variable retains its previous value and is incremented again.

The fact that counter is declared as static means that it is initialized only once when the program starts and retains its value across function calls. If it were declared as a regular local variable, its value would be reset to 0 each time the function is called. 


#include <iostream>

using namespace std;

class Cls{
    public:
        static int counter;

        Cls() {
            cout << "Value of Counter Atm: " << ++counter << endl;
        }
};

int Cls :: counter = 0;

int main() {

    Cls Object;
    Cls Obj2;
    Cls Obj3;
    Cls Obj4;

    return 0;
}

This code defines a class named Cls with a static integer variable counter. The class also has a constructor that increments the counter variable each time an object of the class is created, and prints the current value of counter to the console. The counter variable is initialized outside the class definition and set to 0.

The main() function creates four objects of the Cls class named Object, Obj2, Obj3, and Obj4. Each time an object is created, its constructor is called, which increments the counter variable and prints its current value.

Because the counter variable is declared as static within the Cls class, it is shared by all instances of the class. This means that each time an object is created, the counter variable is incremented, and its new value is retained for the next object.

This demonstrates how static variables can be used to share data between class instances and how they can be used to implement counting mechanisms or other forms of global state within a program.

C++ OO Destructors

In C++, a destructor is a member function of a class that is automatically called when an object of that class goes out of scope, is deleted explicitly, or when the program terminates. A destructor is used to perform any necessary cleanup tasks for the object, such as freeing memory or releasing resources that were allocated during the lifetime of the object.

The destructor has the same name as the class, but is preceded by a tilde (~) symbol. 


#include <iostream>

using namespace std;

class Cls{
    public:
        Cls() {
            cout << "I am from Constructor" << endl;
        }
        ~Cls() {
            cout << "I am from Destructor" << endl;
        }
};

int main() {

    Cls Object;

    return 0;
}

This code defines a class named Cls that has a constructor and a destructor. The constructor and destructor of the class simply print a message to the console.

In the main() function, an object of the Cls class is created using the default constructor. When the object is created, the constructor of the Cls class is called and the message "I am from Constructor" is printed to the console.

When the program exits the main() function, the object of the Cls class goes out of scope and is destroyed. The destructor of the Cls class is automatically called, and the message "I am from Destructor" is printed to the console. 

This demonstrates how constructors and destructors work in C++, and how they are automatically called when objects are created and destroyed.


#include <iostream>
#include <cstdlib>

using namespace std;

class Cls{
    public:
        Cls() {
            system("Calc");
        }
        ~Cls() {
            system("ping google.com");
        }
};

int main() {

    Cls Object;

    return 0;
}

This code defines a class named Cls with a constructor and a destructor. The constructor and destructor of the class call the system() function to execute specific commands.

In the Cls constructor, the system() function is called with the argument "Calc", which launches the Calculator application on Windows. This means that when an object of the Cls class is created, the Calculator application will be launched.

In the Cls destructor, the system() function is called with the argument "ping google.com", which sends a ping request to the Google website. This means that when an object of the Cls class is destroyed, a ping request is sent to the Google website.

In the main() function, an object of the Cls class is created using the default constructor. When the object is created, the Calculator application is launched because of the constructor of the Cls class. When the program exits the main() function, the object of the Cls class goes out of scope and is destroyed, and a ping request is sent to the Google website because of the destructor of the Cls class.

C++ OO Default Constructor Parameters

C++ allows you to specify default parameter values for constructor parameters. This means that if you don't explicitly provide a value for a constructor parameter, the default value will be used. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string name;
        int age;

    public:
        Cls(string def_name = "Michael", int def_age = 40) {
            name = def_name;
            age = def_age;

            cout << "Default name: " << name << endl;
            cout << "Default age: " << age << endl;
        }
};

int main() {

    Cls Object;

    return 0;
}

This code defines a C++ class called Cls with a constructor that has default parameter values for def_name and def_age. The constructor initializes the private member variables name and age with the default values if no arguments are provided when an instance of the Cls class is created.

In the main() function, an instance of the Cls class called Object is created using the default constructor. Since no arguments are provided, the default values for def_name and def_age will be used.

C++ OO Constructor Overloading

Constructor overloading is a feature in C++ that allows a class to have multiple constructors with different parameter lists. In other words, a class can have multiple constructors with the same name, but different arguments. This provides the flexibility to initialize the object's data members in different ways, depending on the constructor used to create the object.

For example, a class may have a default constructor that takes no arguments and initializes data members to default values, and a parameterized constructor that takes arguments to initialize data members to specific values. 

Constructor overloading is useful when you want to provide different ways of initializing an object or when you want to provide default values for some of the object's data members. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string name = "John";
        int age = 30;

    public:
        Cls() {
            cout << "DEFAULT DATA - From Private: " << endl;
            cout << "Default name: " << name << endl;
            cout << "Default age: " << age << endl;
        }

        Cls(string abs_x) {
            cout << "Case when name is changed" << endl;
            name = abs_x;
        }

        Cls(int abs_age) {
            cout << "Case when age is changed" << endl;
            age = abs_age;
        }

        Cls(string x, int y) {
            cout << "We are changing bout of those" << endl;
            name = x;
            age = y;
        }

        void Report() {
            cout << "Real Data Atm: " << endl;
            cout << "Name Atm: " << name<< endl;
            cout << "Age Atm: " << age << endl;
        }
};

int main() {

    Cls Object;

    return 0;
}

Constructor overloading refers to the ability to define multiple constructors in a class, each with a different signature (i.e., different number or types of parameters).

In our example, the Cls class has four different constructors with different sets of parameters. The first one is a default constructor, which sets default values for the name and age variables.

The other three constructors are parameterized constructors. One takes a string parameter to change the value of name, another takes an integer parameter to change the value of age, and the third one takes both a string and an integer parameter to change both values at once.

By providing multiple constructors, the class can be initialized in different ways depending on the specific needs of the program. 


int main() {

    Cls Object("Michael");
    Object.Report();

    return 0;
}

In the previous code, the constructor with the string parameter is being called, which sets the name to "Michael" and leaves the age with the default value of 30. 


int main() {

    Cls Object(55);
    Object.Report();

    return 0;
}

In this case, the constructor Cls(int abs_age) is called with the argument 55, which sets the age variable to 55. Then, the Report() method is called, which prints out the updated value of age and the default value of name


int main() {

    Cls Object("Samantha", 43);
    Object.Report();

    return 0;
}

This code will create an object of class Cls with name set to "Samantha" and age set to 43, using the constructor that takes both string and int arguments. Then, it will call the Report() member function to print the current values of name and age.

C++ OO Constructors

In object-oriented programming, a constructor is a special member function of a class that is used to initialize objects of that class. The constructor is automatically called when an object of the class is created, and it is used to set initial values for the object's data members.

A constructor has the same name as the class and no return type. It can have parameters or no parameters, depending on the needs of the class. The constructor can be used to allocate memory and initialize variables, among other tasks.

Constructors are useful in object-oriented programming because they provide a way to initialize objects when they are created, which helps ensure that the object is in a valid state from the beginning. They also allow objects to be created with different initial values, depending on the needs of the program. 

In C++, if a class does not have a user-defined constructor, the compiler automatically provides a default constructor that has an empty body. However, if a class has one or more data members that need to be initialized to specific values, a user-defined constructor can be defined.


#include <iostream>

using namespace std;

class Cls{
    public:
        Cls() {
            cout << "Automatic Loader" << endl;
        }
};

int main() {

    Cls Object;

    return 0;
}

This code defines a class Cls with a constructor that has no parameters. The constructor is defined with the same name as the class and is executed automatically when an object of the class is created.

In this case, the constructor simply outputs the message "Automatic Loader" to the console using the cout statement.

The main function creates an object of the Cls class using the default constructor. This object is named Object. Since the default constructor has no parameters, no arguments are needed in the constructor call.

When the program runs, the constructor is called automatically, and "Automatic Loader" is output to the console. The program then ends, since there is nothing else to execute. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string name;
        string lastname;
        int age;

    public:
        Cls() {
            name = "Samantha";
            lastname = "Fox";
            age = 43;

            cout << "Name: " << name << endl;
            cout << "Lastname: " << lastname << endl;
            cout << "Age: " << age << endl;
        }
};

int main() {

    Cls Object;

    return 0;
}

This program defines a class Cls with private data members name, lastname, and age. It also defines a default constructor for the class Cls.

The constructor initializes the private data members name, lastname, and age with specific values for a person named Samantha Fox. Then it prints the values of these data members to the console.

In the main function, an object of Cls is created using the default constructor, and the object prints the values of the private data members to the console.

C++ OO Getters and Setters

In object-oriented programming, getters and setters are methods used to access and modify the private members of a class.

A getter is a method that returns the value of a private member variable, allowing outside code to read it, while a setter is a method that sets the value of a private member variable, allowing outside code to modify it.

Using getters and setters provides a controlled way for outside code to access and modify the internal state of a class. This is useful because it allows the class to enforce certain constraints or perform certain actions when its state is modified. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string someString;

    public:
        void getStr() {
            cout << someString << endl;
        }
        void setStr(string x) {
            someString = x;
        }
};

int main() {

    Cls Object;
    Object.setStr("Slavoj Zizek");

    Object.getStr();
    return 0;
}

This is an example of a class with a setter and getter method. The Cls class has a private member variable called someString, and two public methods to access it. The getStr method returns the value of someString and setStr sets the value of someString to a new value passed as a parameter.

In this specific code, an instance of the Cls class is created, and the setStr method is used to set the value of someString to "Slavoj Zizek". Then, the getStr method is called to print the value of someString to the console, which is "Slavoj Zizek".

Getters and setters are useful in object-oriented programming because they provide a way to access and manipulate the private member variables of a class while maintaining control over how the variables are accessed and changed. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string name;
        string lastname;

    public:
        void getStr() {
            cout << name << endl;
            cout << lastname << endl;
        }
        void setStr(string x, string y) {
            name = x;
            lastname = y;
        }
};

int main() {

    Cls Object;
    Object.setStr("Jordan", "Peterson");

    Object.getStr();
    return 0;
}

This rogram that defines a class Cls with two private member variables name and lastname. The class also has two public member functions getStr() and setStr(string x, string y).

The getStr() function is used to display the values of name and lastname on the console.

The setStr(string x, string y) function is used to set the values of name and lastname to the values passed as arguments to the function.

In the main() function, an object of class Cls is created, and the setStr() function is called to set the values of name and lastname. Then, the getStr() function is called to display the values of name and lastname.

C++ OO Private vs Public Access

In C++, private and public are access modifiers that determine the level of access to the members (data and functions) of a class from outside of the class.

public members can be accessed from outside the class by any function or object that has access to an instance of that class. private members, on the other hand, can only be accessed by member functions of the same class.

In general, public members are used to provide an interface for external code to interact with the class, while private members are used for internal implementation details that should not be exposed to external code.

By using access modifiers, we can encapsulate data and functionality within a class, protecting the implementation details and promoting code reusability and maintainability. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string someString = "Constant Stuff";

        void printStr() {
            cout << someString << endl;
        }
};


int main() {

    Cls Object;
    Object.printStr();

    return 0;
}

In this code, the Cls class has a private data member someString and a private member function printStr(). The private members of a class can only be accessed by the member functions of that class and not by any other function or object outside the class.

In the main() function, an object of the Cls class is created. However, when the private member function printStr() is called on this object, the compiler generates an error because it is not allowed to access the private member functions outside the class.

Therefore, the program will not compile and will generate an error message: 


||=== Build: Debug in Main (compiler: GNU GCC Compiler) ===|
C:\Users\user\Desktop\Main\main.cpp|7|warning: non-static data member 
initializers only available with -std=c++11 or -std=gnu++11|
C:\Users\user\Desktop\Main\main.cpp||In function 'int main()':|
C:\Users\user\Desktop\Main\main.cpp|9|error: 'void Cls::printStr()' is private|
C:\Users\user\Desktop\Main\main.cpp|18|error: within this context|
||=== Build failed: 2 error(s), 1 warning(s) (0 minute(s), 0 second(s)) ===|

The code produces a warning and two errors.

The first error indicates that the printStr function is private, which means that it cannot be accessed outside the class. Since main() is trying to call the function on an object of the Cls class, this causes an error.

The second error is a consequence of the first one, and it indicates that the attempt to call the private printStr function is within an invalid context. 


#include <iostream>

using namespace std;

class Cls{
    private:
        string someString = "Constant Stuff";

    public:
        int Num = 5;

        void printStr() {
            cout << someString << endl;
        }

        void printNum() {
            cout << Num << endl;
        }
};

int main() {

    Cls Object;
    Object.printStr();

    Object.printNum();

    return 0;
}

This code defines a class Cls that has two member functions, printStr() and printNum(), and two data members, someString and Num.

The data member someString is declared as private, which means it can only be accessed by member functions of the class itself. On the other hand, the data member Num is declared as public, which means it can be accessed from outside the class as well.

The member function printStr() simply prints out the value of the private data member someString using the cout statement.

The member function printNum() prints out the value of the public data member Num using the cout statement.

In the main() function, an object of the Cls class is created and its member functions printStr() and printNum() are called to print out the values of the private and public data members of the object, respectively.

C++ OO Scope Resolution Operator

In C++, the scope resolution operator (::) is used to access the members of a class that are defined outside of the class definition or to access a member that is hidden by a local variable or parameter.

The scope resolution operator is placed before the name of the member that we want to access and the name of the class or namespace that the member belongs to. 


#include <iostream>

using namespace std;

class Major{
    public:
        string someStr = "Some Stuff";

        void printStr();
};

void Major :: printStr() {
    cout << someStr << endl;
}

int main() {

    Major Object;
    Object.printStr();

    return 0;
}

This code defines a class called Major which has a public member variable someStr of type string initialized to the value "Some Stuff", and a public member function called printStr().

The interesting part of the code is how the member function is defined outside the class using the scope resolution operator (::). This is called defining the function out of line or implementing the function.

In the main function, an object of the Major class is created and its printStr() method is called to print the value of the member variable someStr


#include <iostream>

using namespace std;

class Numbers{
    public:
        int Num = 5;

        void doubleNum();
};

void Numbers :: doubleNum() {
    cout << Num * Num << endl;
}

int main() {

    Numbers Object;
    Object.doubleNum();

    return 0;
}

This code defines a class named Numbers, which has one public data member called Num and one public member function called doubleNum().

The Num data member is initialized to 5. The doubleNum() member function multiplies Num by itself and prints the result to the console.

In the main() function, an object of the Numbers class is created called Object, and the doubleNum() member function of the object is called. This prints the result of Num squared, which is 25, to the console. 


#include <iostream>

using namespace std;

class Numbers{
    public:
        int Num = 5;

        void doubleNum();
};

void Numbers :: doubleNum() {
    cout << (Numbers :: Num) * (Numbers :: Num) << endl;
}

int main() {

    Numbers Object;
    Object.doubleNum();

    return 0;
}

In this code, the doubleNum function of the Numbers class is defined to print the square of the Num member variable. The scope resolution operator :: is used to explicitly refer to the Num variable of the Numbers class within the function definition.

Since Num is a member variable of the Numbers class, it is necessary to use the scope resolution operator to indicate that we are referring to the Num variable of the Numbers class, not some other variable with the same name that might be in a different scope. In this case, the parentheses around Numbers :: Num are not necessary since the multiplication operator has higher precedence than the scope resolution operator, but they do not change the behavior of the code.

When the program is run, an object of the Numbers class is created and the doubleNum function is called on it, resulting in the output of 25, which is the square of the initial value of Num, which is 5.

C++ OO Pointer to Class

A pointer to a class is a variable that holds the memory address of an object of that class. It allows us to access the object's properties and methods from the pointer variable. Pointers to classes are useful in situations where we need to pass objects between functions, allocate memory dynamically, or create linked data structures like linked lists, trees, and graphs.

Pointer to a class is declared with the same syntax as a pointer to any other data type. The only difference is that we need to specify the class type in the declaration. 


#include <iostream>

using namespace std;

class Cls{
    public:
        string someStr = "Content";

        void printStr() {
            cout << someStr << endl;
        }
};

int main() {

    Cls *pTr = new Cls();
    pTr->printStr();

    Cls *Blah = new Cls();
    Blah->printStr();

    return 0;
}

This code demonstrates the use of a pointer to a class. In this example, a class named Cls is defined with a string variable someStr and a function printStr() to print the string. In the main() function, two pointers of type Cls are created dynamically using the new keyword.

The first pointer pTr is assigned the address of a new Cls object, and the printStr() function is called on it using the arrow operator ->.

The second pointer Blah is also assigned the address of a new Cls object, and the printStr() function is called on it using the arrow operator.

This code shows how a pointer to a class can be used to create multiple objects of the class dynamically and call their member functions using the arrow operator.

C++ OO Conflicting Objects


#include <iostream>

using namespace std;

class Cls1{
    public:
        void sameName() {
            cout << "I am from Cls 1" << endl;
        }
};

class Cls2{
    public:
        void sameName() {
            cout << "I am from Cls 2" << endl;
        }
};

int main() {

    Cls1 Object;
    Object.sameName();

    Cls2 Object;
    Object.sameName();

    return 0;
}

||=== Build: Debug in Main (compiler: GNU GCC Compiler) ===|
C:\Users\user\Desktop\Main\main.cpp||In function 'int main()':|
C:\Users\user\Desktop\Main\main.cpp|24|error: conflicting declaration 'Cls2 Object'|
C:\Users\user\Desktop\Main\main.cpp|21|note: previous declaration as 'Cls1 Object'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

C++ OO Data Types


#include <iostream>

using namespace std;

class Books {
    public:
        string Title;
        string Author;
        string Topic;
        char intRegister;
        int ID_Book;

        void printAll() {
            cout << "Title:       \t" << Title       << endl;
            cout << "Author:      \t" << Author      << endl;
            cout << "Topic:       \t" << Topic       << endl;
            cout << "intRegister: \t" << intRegister << endl;
            cout << "ID Book:     \t" << ID_Book     << endl;
        }
};

int main() {

    Books UniqBook;
    UniqBook.Title = "Wires in Space";
    UniqBook.Author = "Sheldon";
    UniqBook.Topic = "String Theory";
    UniqBook.intRegister = 'B';
    UniqBook.ID_Book = 254121;

    UniqBook.printAll();

    return 0;
}

The code defines a class called Books with several data types, such as string and char, and a member function called printAll().

The class Books contains the following data members:

  • Title: A string representing the title of the book.
  • Author: A string representing the author of the book.
  • Topic: A string representing the topic of the book.
  • intRegister: A character representing the book's registration status.
  • ID_Book: An integer representing the book's ID number.

The member function printAll() prints out all the data members of the class to the console.

In the main() function, an object of the Books class is created and its data members are initialized with some values. Then, the printAll() function is called on the UniqBook object to print out its data members to the console.

The code demonstrates the use of a class to define a "book" object with various properties such as title, author, topic, and ID.

This code could be useful in a program that manages a library or book inventory, where book objects need to be created and tracked with specific information.

By defining a book class with different data types for each property, it becomes easier to create and manage book objects and their associated properties within the program. The class can also have member functions that perform operations on book objects such as searching, sorting, or printing.

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...