Lesson # 10. Classes

Дата изменения: 3 декабря 2021

Theory

  • Classes are an expanded concept of data structures: like data structures, they can contain data members, but they can also contain functions as members.
  • An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.
  • Classes are defined using either keyword class or keyword struct, with the following syntax:
  • class class_name {
      access_specifier_1:
        member1;
      access_specifier_2:
        member2;
      ...
    } object_names;
    

    Where class_name is a valid identifier for the class, object_names is an optional list of names for objects of this class. The body of the declaration can contain members, which can either be data or function declarations, and optionally access specifiers.

  • C++ classes encapsulate data and associated functionality into an object:
  • // C++ class: 
    class Cube {
     public:
      double getVolume();
       // ...
     private:
      double length_;
    };
  • Thus, Classes have the same format as plain data structures, except that they can also include functions and have these new things called access specifiers. An access specifier is one of the following three keywords: private, public or protected. These specifiers modify the access rights for the members that follow them.
  • Encapsulation encloses data and functionality into a single unit (called a class)
  • In C++, data and functionality are separated into two separate protections: public and private.
  • The protection level determines the access that «client code» has to the member data or functionality:

  • Public members can be accessed by client code. So, public members are accessible from anywhere where the object is visible. protected members are accessible from other members of the same class (or from their «friends»), but also from members of their derived classes.
  • Private members cannot be accessed by client code (only used within the class itself). So, private members of a class are accessible only from within other members of the same class (or from their «friends»).
  • Example of header-file code (Cube.h):

    #pragma once
    class Cube {
     public:
       double getVolume();
       double getSurfaceArea();
       void setLength(double length);
     private:
       double length_;
    };

    Example of implementation-file code (Cube.cpp):

    #include "Cube.h"
    double Cube::getVolume() {
      return length_ * length_ * length_;
    }
    double Cube::getSurfaceArea() {
       return 6 * length_ * length_;
    }
    void Cube::setLength (double length) {
      length_ = length;
    }

    Example of main-file code (main.cpp):

    #include "Cube.h"
    int main() {
     Cube c;
     c.setLength(3.48);
     double volume = c.getVolume();
     std::cout << "Volume: " << volume << std::endl;
     return 0;
    }

    Arguments passed by …

      Identical to storage, arguments can be passed to functions in three different ways:

    • Pass by value (default)
    • Pass by pointer (modified with *)
    • Pass by reference (modified with &, acts as an alias)

    Values returned by …

    Similarly, values can be returned all three ways as well:

  • Return by value (default)
  • Return by pointer (modified with *)
  • Return by reference (modified with &, acts as an alias)
  • Class destructor

    • When an instance of a class is cleaned up, the class destructor is the last call in a class’s lifecycle.
    • An automatic default destructor is added to your class if no other destructor is defined.
    • The only action of the automatic default destructor is to call the default destructor of all member objects.
    • An destructor should never be called directly. Instead, it is automatically called when the object’s memory is being reclaimed by the system:
    • If the object is on the stack, when the function returns
    • If the object is on the heap, when delete is used
    • To add custom behavior to the end-of-life of the function, a custom destructor can be defined as:
    • A custom destructor is a member function.
    • The function’s destructor is the name of the class, preceded by a tilde ~.
    • All destructors have zero arguments and no return type.
    • Cube::~Cube(); // Custom destructor

    Labs and tasks

    Lab 1:
    To do: Create a Rectangle class and an object (i.e., a variable) of this class, called rect.
    1) The class should contain four members:
    — two data members of type int (member width and member height) with private access,
    — and two member functions with public access: the functions set_values and area.

    2) Define two member functions: set_values function should set the variables width and height to the values; are function should return width*height.

    3) Create a Custom default constructor to have the initial values for width and height (set them to 5).

    4) Set the width and height values for the rect object and print out the information about this object.

    Expected output:

    Lab 1:
    please, enter width:
    >> 20
    please, enter height:
    >> 2
    rect area: 40
    

    ✍ Algorithm:

    • To do the lab create empty console application with a name Lesson9. Add files: mainLab1.cpp, ImpLab1.cpp, HeaderLab1.h.
    • Include all the needed libraries and files.
    • 1.Create a Rectangle class:

    • The class must be defined inside a header file. So, open the header file and add the code to define the class with two private data members, they are width and height; and two member functions with public access, they are set_values and area.
    • class Rectangle {
      private:
      	int width, height;
      public:
      	void set_values(int, int);
      	int area(void);
      } ;
      
    • Here Rectangle is the class name (i.e., the type).
    • The functions set_values and area have a public access, it means that they can be accessed from inside the main function by simply inserting a dot (.) between object name and member name (e.g. rect.set_values…).
    • width and height members cannot be accessed from outside the class, since they have private access and they can only be referred to from within other members of that same class.
    • 2. Define two member functions:

    • Since members width and height have private access, access to them from outside the class is not allowed. So, we should define a member function to set values for those members within the object: the member function set_values.
    • Let’s create the definition of set_values member function. We’re going to have a member of a class outside the class itself, so, we have to use scope operator (::, two colons). You should create the definition of the function inside the implementation file:
    • void Rectangle::set_values(int x, int y) {
      	width = x;
      	height = y;
      }
      
      The scope operator (::) specifies the class to which the member being defined belongs, granting exactly the same scope properties as if this function definition was directly included within the class definition. For example, the function set_values has access to the variables width and height, which are private members of class Rectangle, and thus only accessible from other members of the class, such as this.
    • The second member function, that is area function, we’re going to have inside the class itself, just to try different ways of definition. So, return to the header file and add the definition inside the class next to int area(void) statement:
    • int area() {return width*height;}
      
      The function is automatically considered an inline member function by the compiler.

      3) Create a Custom default constructor to have the initial values for width and height (set them to 5).

    • Add the declaration of a Custom default constructor inside the public access of the class (header file):
    • public: // you had this code
      	Rectangle(); 
      	void set_values(int, int); // you had this code
      //...
      
    • Open your implementation file to add the definition of that constructor:
    •  Rectangle::Rectangle() {
      	width = 5;
      	height = 5;
      }
      

      4) Set the width and height values for the rect object and print out the information about this object:

    • Open a main file in the editor window. You should create an object (i.e., a variable) of the Rectangle class, called rect. And after this, ask user to input the width and height values. Call the set_values function and output the result:
    • int main() {
      	Rectangle rect;
      	int w, h;
      	cout << "please, enter width:\n";
      	cin >> w;
      	cout << "please, enter height:\n";
      	cin >> h;
      	rect.set_values(w, h);
      	cout << "rect area: " << rect.area() << endl;
      	system("pause");
      	return 0;
      }
      
    • Run the program and check the output.
    Task 1:

    To do: Create a LessonDate class to output the date of a lesson.

    1) The class should contain the following members:
    — three data members of type int with private access, they are day, month and year;
    — and two member functions with public access:
    void setDate(int, int, int); to set the date for the next lesson and
    void getDate(); to print out the date of the lesson.

    2) Create a three argument constructor to have the initial values for date; in the constructor body call the setDate function to set the date.

    3) Inside the main function create an object (i.e., a variable) of this class, called objLesson. Set the values of three parameters — day, month and year. Print out the information about this object.

    Expected output:

    Task 1:
    Lesson date: 11.11.2021
    Please, enter day, then month and year of the next lesson
    28
    12
    2020
    Lesson date: 28.12.2020
    

    [Solution and Project name: Lesson_10task1, file name L10Task1main.cpp, L10Task1imp.cpp, L10Task1.h]

    Lab 2:
    To do: Create a Cube class.
    1) The class should contain four members:
    length_ data member of type double with private access,
    — three member functions with public access: the functions double getVolume(); , double getSurfaceArea(); and void setLength(double length);.

    2) Give the definitions (implementations) of those functions:
    getVolume() function have to calculate a volume of the cube: length_ * length_ * length_;
    getSurfaceArea() function have to calculate a Surface Area of the cube: 6 * length_ * length_;;
    setLength(double length) function have to set the value for the cube length.

    3) Create a Custom default constructor to have the initial values for length_ (set it to 1).

    4) Inside the main cpp file create double cube_on_stack() function to create an object (i.e., a variable) of this class and get volume of it. This object will be in a stack memory. Also, inside the main function create a new cube of length 10 which is going to be in heap memory. Set the values. Print out the information about those objects.

    5) Create a custom destructor to delete an information about the cube.

    Expected output:

    Lab 2:
    Volume of c cube:
    Destructor called for cube with length 2
    Volume of c cube in the heap memory: 27
    Destructor called for cube with length 3
    

    ✍ Algorithm:

    • To do the lab create an empty console application with a name Lesson9Lab2. Create new files: mainLab2.cpp, ImpLab2.cpp, HeaderLab2.h.
    • Include all the needed libraries and files.
    • 1.The class should contain four members:

    • The interface of the class must be inside a header file. So, open the header file and add the code to define the class with one private data member, it is length_ of a cube; and three member functions with public access, they are getVolume(), getSurfaceArea() and setLength(double length).
    • class Cube
      {
      public:
      	double getVolume();
      	double getSurfaceArea();
      	void setLength(double length);
      private:
      	double length_;
      };
      
    • Here Cube is the class name (i.e., the type.
    • The functions getVolume(), getSurfaceArea() and setLength(double length) have a public access, it means that they can be accessed from anywhere where the object is visible.
    • length_ member cannot be accessed from outside the class, since it has a private access and it can only be accesses from within the class itself.
    • 2. Give the definitions (implementations) of the functions:

    • Let’s create the definition of setLength member function. We’re going to have a member of a class outside the class itself, so, we have to use scope operator (::, two colons). You should create the definition of the function inside the implementation cpp file:
    • void Cube::setLength(double length) {
      	length_ = length;
      }
      
      The scope operator (::) specifies the class to which the member being defined belongs, granting exactly the same scope properties as if this function definition was directly included within the class definition. For example, the function setLength has access to the variable length_, which is a private member of the class Cube, and thus only accessible from other members of the class, such as this.
    • The second and th third member functions are getVolume and getSurfaceArea function, we’re going to have them inside the implementation file too:
    • double Cube::getVolume() {
      	return length_ * length_ * length_;
      }
      double Cube::getSurfaceArea() {
      	return 6 * length_ * length_;
      }
      
      The getVolume function will return the volume of that cube, for which this function will be called.
      The getSurfaceArea function will return the surface area of that cube, for which this function will be called.

      3) Create a custom default constructor to have the initial values for length_ (set it to 1).

    • Add the declaration of a custom default constructor inside the public access of the class (header file):
    • public: // you had this code
      	Cube(); 
              //...
      
    • Open your implementation file to add the definition of that constructor:
    • Cube::Cube() {
      	length_ = 1;
      }

      4) Inside the main cpp file create double cube_on_stack() function to create an object (i.e., a variable) of this class and get volume of it:

    • Open a main file in the editor window. Before the main function add the code to create cube_on_stack() which will create the object of Cube class and return the volume of this object:
    • double cube_on_stack() {
      	Cube c;
      	c.setLength(2);
      	return c.getVolume();
      }
      int main() {
       // ...
      }
      
    • Inside the main function ptint out the message and call that function:
    • int main() {
        cout << "Volume of first cube: " << endl;
        cube_on_stack();
      }
      

      Also, inside the main function create a new cube of length 10 which is going to be in heap memory. Set the values. Print out the information about those objects.

    • Then, inside the main function you should create a new cube of length 10 which is going to be in heap memory:
    •   Cube * pcube = new Cube;
        pcube->setLength(3);
        cout << "Volume of c cube in the heap memory: " << pcube->getVolume() << endl;
      
      To have an object to be stored in the heap memory you should use star (*) sign. Such objects or instances to the objects can be used together with arrow operator to access their members.

      5) Create a custom destructor to delete an information about the cube.

    • Open header file to declare a class destructor, which will be called to clean up the instance of the class. Add it right after the class constructor to the public protection level:
    • ~Cube();
      
    • Add the implementation for that destructor inside implementation file:
    • Cube::~Cube(){
      	cout << "Destroyed cube with length " << length_;
      }
      
    • Return to the main file and add the delete keyword to clean up the memory of the pcube object:
    • // ...
      delete pcube;
      
      If the object is on the heap, the destructor is only called when that delete keyword is used, to reclaim that memory that the object was using.
    • Run the program and check the output.
    Task 2:

    To do: Create a Student class to output information about the students.

    1) The class should contain the following members:
    — two data members of type string with private access, they are name and surname, and one data member of type int - age;
    — and two member functions with public access:
    void set_info(string, string, int); to set the information about the student
    and void get_info(void); to print out the information.

    2) Create a three argument constructor to have the initial values for student.

    3) Inside the main cpp file create void student_on_stack() function to create an object (i.e., a variable) of this class and get volume of it. This object will be in a stack memory. Also, inside the main function create a new student with some info about, which is going to be in a heap memory. Print out the information about those objects.

    4) create a custom destructor to delete an information about student.

    Expected output:

    Task 2:
    info about student1: name: Johnsurname: Ivanovage: 20
    Destructor called for Student Ivanov
    info about student2: name: Petersurname: Panage: 17
    Destructor called for Student Pan
    

    [Solution and Project name: Lesson_10task2, file name L10Task2main.cpp, L10Task2imp.cpp, L10Task2.h]

    Task 3:

    To do: Create a BookShop class to store and output an information about the selling books.

    1) The class should contain the following members:
    data members with private access:
    title_ (a title of a book) of type string;
    author_ (an author of a book) of type string;
    _price (a price of a book) of type double;
    _discount (a discount for a price of a book) of type int.
    data members with public access:
    - void getInfo() function to print out the information about the book;
    - void set_info(string title, string author, double price, int discount) function to set the information about the book;
    - double getTotalPrice() function calculate the price of a book considering the discount (price - (price * discount)).

    2) Create a four argument constructor to have the initial values for books.

    3) Create two objects and print out the information about those objects. Print out the info about the price considering the discount.

    Expected output:

    Task 3:
    // book 1 info:
    Dostoevsky Demons 205 roubles, discount 0.05, total price 194,75 roubles
    // book 2 info:
    Kuprin Duel 125 roubles, discount 0.1, total price 112,5 roubles
    

    [Solution and Project name: Lesson_10task3, file name L10Task3main.cpp, L10Task3imp.cpp, L10Task3.h]


    Lab 3:
    To do: Create a base class called Professor.

    1) The class should contain the following members:
    - Name_ data member of string type with protected access,
    - Age_ data member of int type with private access,
    - StudentsCount_ data member of int type with private access,
    - Rating_ data member of int type with protected access,
    - Subjects_ data member of list type with private access,

    Four member functions with public access:
    - void getInfo(); (to output information about a professor),
    - void addSubject(); (to add a new subject to the list),
    - void addStudent(); and void setRemoveStudent(); (to increase or decrease the counter of the students);
    - void checkRating(); (to check a rating and output some message).
    2) Create a derived class called ItProfessors. The class should have its own methods called void MoreInfo(); (to output an addition information) and incRating(); (to increase a rating and output some info).

    3) Create a derived class called MathProfessors. The class should have its own methods called void MoreInfo(); (to output an addition information) and incRating(); (to increase a rating and output some info) .

    4) Create an object of the base class inside the main function. Call all existing methods of the class for that object.

    5) Create object of the derived classees inside the main function. Call all existing methods of the class.

    Expected output:

    Lab 3:
    name = Ivanov
    age = 56
    subjects =
    Basics_of_programming Math Geometry
    Ivanov is good in programming
    Johnson is good in programming
    IT area professor Johnson's rating was increased and now it is 1
    IT area professorJohnson' rating was increased and now it is 2
    IT area professorJohnson' rating was increased and now it is 3
    Johnson has good rating
    Ivanov has no enouph good rating
    

    ✍ Algorithm:

    • To complete the task you should create a L10lab3.cpp file only. All code should be inside one file.
    • Include all the needed libraries:
    • #include<iostream>
      #include<string>
      #include<list>
      using namespace std;

      Create a base class:

      class Professor {
        private:
      	// ...
        protected:
      	// ...
        public:
              // ...
      };
    • Data members should be declared inside the private area:
    •         int Age_;
      	int StudentsCount_;
      	list<string> Subjects_;
    • Since the Name_ and Rating_ members must be accessible from the derived class, we should declare them inside the protected area:
    •        string Name_;
             int Rating_;
    • Now we're going to create a constructor with three arguments. It should be placed inside the public area:
    • Professor(string name, int age, int count) {
      	Name_ = name;
      	Age_ = age;
      	StudentsCount_ = count;
              int Rating_ = 0;
      }
    • Let's create a getInfo() method to output all the information. To output the list we're going to use a for : loop, iterating over the elements of the list. The method should be inside the public area:
    • void getInfo() {
      		cout << "name = " << Name_<< endl;
      		cout << "age =" << Age_<< endl;
      		for (string subj : Subjects_) {
      			cout << subj << " ";
      		}
                      cout << endl;
      	}
    • After, let's create addStudent() method to increase a counter of the students. The method should be placed inside the public area, as well as other methods.
    • void addStudent() {
      			StudentsCount_++;
      		}
    • The same way create a method to decrease a counter of the students:
    • void removeStudent() {
      			if (StudentsCount_>0)
      			StudentsCount_--;
      		}
    • We're going to create a member to add a new value to the list of subjects. In order to do it we need to use push_back() method of the list type:
    •            void addSubject(string subject) {
      			Subjects_.push_back(subject);
       
      		}

      Create a derived class:

    • After the closing curly brace of the base class, we're going to add the code of a derived class. Let's declare the class:
    • class ItProfessor:public Professor {
      public:
      // ...
      };
    • This class inherits all the members of the base class. But we need to have public default constructor to have them accessible outside of this class. Let's add the code of constructor in the public area:
    • ItProfessor(string name, int age, int count):Professor (name, age, count){
      	}
    • The derived class can have its own specific member. Let's add it in the public area as well:
    • void moreInfo(){
      		cout <<Name_ <<" is good in programming" << endl;;
      	}
    • We're going to create one more derived class with a name MathProfessor:
    • class MathProfessor:public Professor {
      public:
        // ...
      };
    • We need to have public default constructor to have the members from the base class accessible outside of this class:
    • MathProfessor(string name, int age, int count):Professor (name, age, count){
      	}
    • Let's create the objects of these classes inside the main function:
    • ItProfessor IvProfessor("Ivanov", 56,5);
      ItProfessor JohnProfessor("Johnson", 50, 5);
      MathProfessor PeterProfessor("Peterson", 50, 5);
    • We are able to evoke all of those methods which were implemented within the base class:
    • 	IvProfessor.addSubject("Math");
      	IvProfessor.addSubject("Geometry");
      	IvProfessor.addStudent();
      	IvProfessor.getInfo();
      	IvProfessor.moreInfo();
      	JohnProfessor.moreInfo();
    • Now, let's create an object of the base class:
    •          Professor pr("Petrov", 45, 6);
      	// pr.moreInfo(); error! is not available for the base class
    • Both of the derived classes should have a method to increase a rating. The names of the methods should be the same, but their implementations must be different. We can call it Polymorphism. Let's add the following method to ItProfessor class:
    • void incRating() {
      	cout << " IT area professor";
      	Rating_++;
      	cout << Name_ << "'s rating was increased and now it is " << Rating_ << endl;
      }
    • The same method for MathProfessor class will be a bit different:
    • void incRating() {
      	cout << " Math area professor";
      	Rating_++;
      	cout << Name_ << "' rating is " << Rating_ << endl;
      	}
    • Now, let's add a method to check the rating. Since the method must be accessible in the derived classes, it should be defined inside the base class - Professor:
    • void checkRating() {
      	if (Rating_ < 3) {
      		cout << Name_<< " has no enouph good rating" << endl;
      	}
      	else {
      		cout << Name_ << " has good rating" << endl;
      		}
      	}
    • Now, let's increase the rating of the object by calling the incRating() method:
    • JohnProfessor.incRating();
      JohnProfessor.incRating();
      JohnProfessor.incRating();
    • In order to call the checkRating() method we should use the pointers. Inside the main function we're going to assign address of the object of the derived class to a pointer of the base class:
    • Professor *p1 = &JohnProfessor;
      Professor *p2 = &IvProfessor;
    • Now, we can call the method to check the rating:
    • p1->checkRating();
      p2->checkRating();
    • Run the program. That's all.
    Task 4:
    1) Create a base class called Animals.
    The class should contain the following data members:
    - Name_ data member of string type with protected access,
    - Class_ data member of int type with private access (class of vertebrate animal),
    - Countries_ data member of List type with private access (countries of residence),
    - Population_ data member of int type with private access;
    The class should contain the following member functions with public access:
    - void getInfo(); (to output information about an animal),
    - void addCountry(); (to add a new country to the list and output new list),
    - list getAnimalsCountry(); (to return the list of animals by the specified country) .
    2) Create a derived class called AfricanAnimals. Class inherits all the members of Animals class. The class should have its own method called void MoreInfo(); (to output an addition information about African animals).

    3) Create an object of the base class inside the main function. Call all existing methods of the class for that object.

    4) Create an object of the derived class inside the main function. Call all existing methods of the class.

    Note: Create a header file for classes.

    Expected output:

    Task 4:
    name = polar_bear
    сlass = mammal
    population =  20000
    countries = Russia USA Canada
    what country to add? Greenland
    new info about countries = Russia USA Canada Greenland
    name = elephant
    сlass = mammal
    population =  20000
    countries = Africa India
    more info about African animals: The fauna of Africa varies greatly depending on the climatic zone.
    
    the animals from whicj country? USA
    polar_bear
    

    [Solution and Project name: file names L10Task4main.cpp, L10Task4.h]

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *

*
*


Вставить формулу как
Блок
Строка
Дополнительные настройки
Цвет формулы
Цвет текста
#333333
Используйте LaTeX для набора формулы
Предпросмотр
\({}\)
Формула не набрана
Вставить