OOP (basics)
Classes and objects
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();
}
Write a Car class.
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;
}
Create a class called Student_group_with_tutor.
#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;
}
Create a class called "Faculty_student_group".
#include <iostream>
class A
{
private:
int value;
public:
A(int);
};
A::A(int v) : value(v) {}