Home


Lesson 7

OOP (basics)

Classes and objects

Task 0

Describe new language elements.


#include <cmath>

class Square
{
    private:
        int length;
    public:
        Square();
        Square(int);
        double diagonal();
};

Square::Square()
{
    length = 1;
}

Square::Square(int length)
{
    this->length = length;
}

double Square::diagonal()
{
    return length * std::sqrt(2);
}

int main()
{
    Square s;
    std::cout << s.diagonal();
}
Task 1

Write a Car class.


Pojedynczny schemat UML dla klasy Car
Task 2

Create a class called "Student_group", which will store an array of structure "Student".

Let's assume student could not be "shared" between 2 students group, create a destructor.


#include <iostream>

class A
{
    private:
        int *p;
    public:
        A();
        ~A();
};

A::A()
{
    this->p = new int[100];
}

A::~A()
{
    delete[] this->p;
}
Task 3

Create a class called Student_group_with_tutor.

  1. Use inheritance
  2. Add a string field (for tutor's name).
  3. Create a proper destructor.

#include <iostream>

class A
{
    public:
        int value;
};
class B: public A
{

};

int main() {
    B* b = new B();
    b->value = 10;
    std::cout << b->value;
    delete b;
}

    
Task 4

Create a class called "Faculty_student_group".

  1. Use inheritance
  2. Add an enum, that represents a faculty.
  3. Use "->".
  4. Clear RAM properly.

#include <iostream>

class A
{
    private:
        int value;
    public:
        A(int);
};

A::A(int v) : value(v) {}