24. Exercises – Modern C++ for Absolute Beginners: A Friendly Introduction to C++ Programming Language and C++11 to C++20 Standards

© Slobodan Dmitrović 2020
S. DmitrovićModern C++ for Absolute Beginnershttps://doi.org/10.1007/978-1-4842-6047-0_24

24. Exercises

Slobodan Dmitrović1 
(1)
Belgrade, Serbia
 

24.1 Class Instance

Write a program that defines an empty class called MyClass and makes an instance of MyClass in the main function.
class MyClass
{
};
int main()
{
    MyClass o;
}

24.2 Class with Data Members

Write a program that defines a class called MyClass with three data members of type char, int, and bool. Make an instance of that class inside the main function.
class MyClass
{
    char c;
    int x;
    bool b;
};
int main()
{
    MyClass o;
}

24.3 Class with Member Function

Write a program that defines a class called MyClass with one member function called printmessage(). Define the printmessage() member function inside the class and make it output the “Hello World” string. Create an instance of that class and use the object to call the class member function .
#include <iostream>
class MyClass
{
public:
    void printmessage()
    {
        std::cout << "Hello World.";
    }
};
int main()
{
    MyClass o;
    o.printmessage();
}

24.4 Class with Data and Function Members

Write a program that defines a class called MyClass with one member function called printmessage(). Define the printmessage() member function outside the class and have it output the "Hello World." string. Create an instance of that class and use the object to call the member function .
#include <iostream>
class MyClass
{
public:
    void printmessage();
};
void MyClass::printmessage()
{
    std::cout << "Hello World.";
}
int main()
{
    MyClass o;
    o.printmessage();
}

24.5 Class Access Specifiers

Write a program that defines a class called MyClass with one private data member of type int called x and two member functions. The first member function called setx(int myvalue) will set the value of x to its parameter myvalue. The second member function is called getx(), is of type int and returns a value of x. Make an instance of the class and use the object to access both member functions.
#include <iostream>
class MyClass
{
private:
    int x;
public:
    void setx(int myvalue)
    {
        x = myvalue;
    }
    int getx()
    {
        return x;
    }
};
int main()
{
    MyClass o;
    o.setx(123);
    std::cout << "The value of x is: " << o.getx();
}

24.6 User-defined Default Constructor and Destructor

Write a program that defines a class called MyClass with a user-defined default constructor and user-defined destructor. Define both constructor and destructor outside the class. Both member functions will output a free to choose the text on the standard output. Create an object of a class in function main.
#include <iostream>
class MyClass
{
public:
    MyClass();
    ~MyClass();
};
MyClass::MyClass()
{
    std::cout << "Constructor invoked." << '\n';
}
MyClass::~MyClass()
{
    std::cout << "Destructor invoked." << '\n';
}
int main()
{
    MyClass o;
}

24.7 Constructor Initializer List

Write a program that defines a class called MyClass, which has two private data members of type int and double. Outside the class, define a user-provided constructor accepting two parameters. The constructor initializes both data members with arguments using the initializer. Outside the class, define a function called printdata() which prints the values of both data members.
#include <iostream>
class MyClass
{
private:
    int x;
    double d;
public:
    MyClass(int xx, double dd);
    void printdata();
};
MyClass::MyClass(int xx, double dd)
    : x{ xx }, d{ dd }
{
}
void MyClass::printdata()
{
    std::cout << " The value of x: " << x << ", the value of d: " << d << '\n';
}
int main()
{
    MyClass o{ 123, 456.789 };
    o.printdata();
}

24.8 User-defined Copy Constructor

Write a program that defines a class called MyClass with arbitrary data fields. Write a user-defined constructor with parameters that initializes data members. Write a user-defined copy constructor which copies all the members. Make one object of the class called o1 and initialize it with values. Make another object of a class called o2 and initialize it with object o. Print data for both objects.
#include <iostream>
class MyClass
{
private:
    int x;
    double d;
public:
    MyClass(int xx, double dd);    // user-provided constructor
    MyClass(const MyClass& rhs);   // user-defined copy constructor
    void printdata();
};
MyClass::MyClass(int xx, double dd)
    : x{ xx }, d{ dd }
{}
MyClass::MyClass(const MyClass& rhs)
    : x{ rhs.x }, d{ rhs.d }
{}
void MyClass::printdata()
{
    std::cout << "X is: " << x << ", d is: " << d << '\n';
}
int main()
{
    MyClass o1{ 123, 456.789 }; // invokes a user-provided constructor
    MyClass o2 = o1; // invokes a user-defined copy constructor
    o1.printdata();
    o2.printdata();
}

24.9 User-defined Move Constructor

Write a program that defines a class with two data members, a user-provided constructor, a user-provided move constructor, and a member function that prints the data. Invoke the move constructor in the main program. Print the moved-to object data fields.
#include <iostream>
#include <string>
class MyClass
{
private:
    double d;
    std::string s;
public:
    MyClass(double dd, std::string ss) // user-provided constructor
        : d{ dd }, s{ ss }
    {}
    MyClass(MyClass&& otherobject) // user-defined move constructor
        :
        d{ std::move(otherobject.d) }, s{ std::move(otherobject.s) }
    {
        std::cout << "Move constructor invoked." << '\n';
    }
    void printdata()
    {
        std::cout << "The value of doble is: " << d << ", the value of string is: " << s << '\n';
    }
};
int main()
{
    MyClass o1{ 3.14, "This was in object 1" };
    MyClass o2 = std::move(o1); // invokes the move constructor
    o2.printdata();
}

24.10 Overloading Arithmetic Operators

Write a program that overloads arithmetic operator – in terms of a compound arithmetic operator -=. Print out the values of the resulting object member fields.
#include <iostream>
class MyClass
{
private:
    int x;
    double d;
public:
    MyClass(int xx, double dd)
        : x{ xx }, d{ dd }
    {
    }
    void printvalues()
    {
        std::cout << "The values of x is: " << x << ", the value of d is: " << d;
    }
    MyClass& operator-=(const MyClass& rhs)
    {
        this->x -= rhs.x;
        this->d -= rhs.d;
        return *this;
    }
    friend MyClass operator-(MyClass lhs, const MyClass& rhs)
    {
        lhs -= rhs;
        return lhs;
    }
};
int main()
{
    MyClass myobject{ 3, 3.0 };
    MyClass mysecondobject{ 1, 1.0 };
    MyClass myresult = myobject - mysecondobject;
    myresult.printvalues();
}