
Dynamic Memory Management and Object-Oriented Programming
Explore the concepts of dynamic memory allocation, pointers, and classes in C++. Learn how to create, use, and delete dynamic memory objects, along with examples showcasing their implementation. Discover the principles of object-oriented programming and how they are applied in code snippets.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
new delete. new : int* ptr = new int{ 5 }; cout << "*ptr = " << *ptr << endl; delete . delete ptr; 2
#include <iostream> int* createPtr(int value) { int* ptr{ new int{value} }; return ptr; } void usePtr() { int* obj = createPtr(5); cout << *obj << endl; // 5 delete obj; // } int main() { usePtr(); } 3
int* arrA = new int[5]; int* arrB = new int[3] {1, 2, 3}; delete[] arrA; delete[] arrB; 4
#include <iostream> int main() { int rows = 3; // int cols = 2; // int** numbers{ new int* [rows] }; // // for (int i{}; i < rows; i++) { numbers[i] = new int[cols] {}; } // for (int i{}; i < rows; i++) { delete[] numbers[i]; } delete[] numbers; } 5
#include <iostream> class Person { public: string name; unsigned int age; void print() { cout << "Name: " << name << "\tAge: " << age << endl; } }; int main() { Person person; // person.name = "Ivan"; person.age = 25; // person.print(); 6 }
#include <iostream> class Person { public: string name; unsigned int age; void print() { cout << "Name: " << name << "\tAge: " << age << endl; } }; int main() { Person person; Person* ptr = &person; // ptr->name = "Ivan"; ptr->age = 22; ptr->print(); // cout << "Name: " << person.name << "\tAge: " << person.age << endl; 7 }
#include <iostream> class Person { public: string name; unsigned int age; void print() { cout << "Name: " << name << "\tAge: " << age << endl; } Person(string p_name, unsigned p_age) { name = p_name; age = p_age; cout << "Person has been created" << endl; } }; int main() { Person ivan("Ivan", 25); // - ivan.print(); 8 }
#include <iostream> class Person { string name{}; unsigned age{}; public: void print() { cout << "Name: " << name << "\tAge: " << age << endl; } Person(string p_name, unsigned p_age) { name = p_name; age = p_age; cout << "First constructor" << endl; } Person(string p_name) : Person(p_name, 25) { cout << "Second constructor" << endl; } Person() : Person(string("Ivan")) { cout << "Third constructor" << endl; } }; int main() { Person ivan; // Person() ivan.print(); 9 }
#include <iostream> class MyClass { public: int** arr; unsigned int rows; unsigned int cols; MyClass(unsigned int rows, unsigned int cols) { this->cols = cols; this->rows = rows; arr = new int* [rows]; // // for (int i{}; i < rows; i++) { arr[i] = new int[cols]; } } ~MyClass() { for (int i{}; i < rows; i++) { delete[] arr[i]; } delete[] arr; } }; int main() { MyClass* myClass = new MyClass(5, 7); } 10
#include <iostream> class Person { string name; unsigned age; public: // Person(string p_name = "Ivan", unsigned p_age = 25) { name = p_name; age = p_age; } void print() { cout << "Name: " << name << "\tAge: " << age << endl; } }; 11
#include <iostream> class Person { public: string name; unsigned int age; void print() { cout << "Name: " << name << "\tAge: " << age << endl; } Person(string p_name, unsigned p_age) { name = p_name; age = p_age; } }; int main() { Person *ivan = new Person("Ivan", 25); // name, age print ivan->name = "Vasya"; ivan->age = 1001; ivan->print(); // Name: Vasya Age: 1001 12 }
#include <iostream> class Person { private: string name; unsigned age; public: void print() { cout << "Name: " << name << "\tAge: " << age << endl; } Person(string p_name, unsigned p_age) { name = p_name; age = p_age; } }; int main() { Person *ivan = new Person("Ivan", 25 ); // print ivan->print(); // Name: Ivan Age: 25 // name age // ivan->name = ""; // ivan->age = 1001; 13 }
class Person { private: string name; unsigned age; public: Person(string p_name, unsigned p_age) { name = p_name; if (p_age >= 0 && p_age < 110) age = p_age; // , else age = 18; } void print() { cout << "Name: " << name << "\tAge: " << age << endl; } void setAge(unsigned p_age) { if (p_age > 0 && p_age < 110) age = p_age; } string getName() { return name; } unsigned getAge() { return age; } }; 14
this #include <iostream> class Point { private: int x; int y; public: Point(int x, int y) { this->x = x; this->y = y; } void showCoords() { cout << "Point x: " << this->x << "\t y: " << y << endl; } }; int main() { Point p1{ 2, 3 }; p1.showCoords(); } 15
this class Point{ private: int x; int y; public: Point(int x, int y) { this->x = x; this->y = y; } void showCoords() { cout << "Coords x: " << x << "\t y: " << y << endl; } Point& move(int x, int y) { this->x += x; this->y += y; return *this; } }; int main() { Point p1{ 2, 5 }; p1.showCoords(); // Point x: 2 y: 5 p1.move(1, 5).move(1, 1); p1.showCoords(); // Point x: 4 y: 11 16 }
class Person { private: string name; unsigned age; static unsigned int count; // - Person public: Person(string p_name, unsigned p_age) { count++; // name = p_name; age = p_age; } void print_count() { cout << "Created " << count << " objects" << endl; } }; unsigned int Person::count; int main() { Person tom{ "Tom", 20 }; Person bob{ "Bob", 25 }; Person sam{ "Sam", 30 }; tom.print_count(); bob.print_count(); sam.print_count(); } 17
#include <iostream> class MyMathClass { public: static unsigned int Max(unsigned int a, unsigned int b) { return a > b ? a : b; } }; int main() { cout << MyMathClass::Max(2, 5); } 18
enum class _ { _1, _2, _N }; enum class Day { Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday } Day today = Day::Thursday; 19
#include <iostream> enum class Operation { Add, Subtract, Multiply }; void calculate(int n1, int n2, Operation op) { switch (op) { case Operation::Add: cout << n1 + n2 << endl; break; case Operation::Subtract: cout << n1 - n2 << endl; break; case Operation::Multiply: cout << n1 * n2 << endl; break; } } int main() { calculate(10, 6, Operation::Add); // 16 calculate(10, 6, Operation::Subtract); // 4 calculate(10, 6, Operation::Multiply); // 60 20 }
class Person { public: void print() const { cout << "Name: " << name << "\tAge: " << age << endl; } string name; unsigned int age; }; class Employee { public: void print() const { cout << "Name: " << name << "\tAge: " << age << endl; } string name; unsigned int age; string company; }; 21
class Person { public: void print() const { cout << "Name: " << name << "\tAge: " << age << endl; } string name; unsigned int age; }; class Employee : public Person { public: string company; }; 22
protected 23
protected, class Person { public: Person(string name, unsigned age) { this->name = name; this->age = age; } void virtual print() { cout << "Name: " << name << "\tAge: " << age << endl; } protected: string name; unsigned age; }; class Employee : public Person { public: Employee(string name, unsigned age, string company) : Person(name, age) { this->company = company; } void print() { cout << name << " works in " << company << endl; // ! } private: string company; 24 };
#include <iostream> class Camera { public: void makePhoto() { cout << "making photo" << endl; } }; class Phone { public: void makeCall() { cout << "making call" << endl; } }; class Smartphone : public Phone, public Camera { }; int main() { Smartphone myPhone; myPhone.makePhoto(); myPhone.makeCall(); 25 }
() class Person { public: Person(string name) : name{ name } { } void print() { cout << "Person " << name << endl; } private: string name; }; class Employee : public Person { public: Employee(string name) : Person{ name } { } }; int main() { Employee employee{ "Ivan" }; employee.print(); // Person: Ivan // Person person1{ employee }; // person1.print(); // Person: Ivan Person person2{ "Vasya" }; person2 = employee; // person2.print(); // Person: Ivan 26 }
#include <iostream> class Shape { public: virtual double getSquare() const = 0; // virtual double getPerimeter() const = 0; // }; 27
class Rectangle : public Shape { // public: Rectangle(double w, double h) : width(w), height(h) { } double getSquare() const override { return width * height; } double getPerimeter() const override { return width * 2 + height * 2; } private: double width; // double height; // }; 28
class Circle : public Shape { // public: Circle(double r) : radius(r) { } double getSquare() const override { return radius * radius * 3.14; } double getPerimeter() const override { return 2 * 3.14 * radius; } private: double radius; // }; 29
int main() { Rectangle rect{ 30, 50 }; Circle circle{ 30 }; cout << "Rectangle square: " << rect.getSquare() << endl; cout << "Rectangle perimeter: " << rect.getPerimeter() << endl; cout << "Circle square: " << circle.getSquare() << endl; cout << "Circle perimeter: " << circle.getPerimeter() << endl; } 30
#include <iostream> class Counter { public: Counter(int val) { value = val; } void print() { std::cout << "Value: " << value << std::endl; } Counter operator + (const Counter& counter) const { return Counter{ value + counter.value }; } private: int value; }; int main() { Counter c1{ 20 }; Counter c2{ 10 }; Counter c3 = c1 + c2; c3.print(); // Value: 30 31 }