
Explore C++ Class Basics: Constructors, Objects, and Methods
Dive into the fundamentals of C++ classes with a focus on constructors, objects, and methods. Learn how to create and initialize objects, define class members, and implement member functions. Follow along with examples and code snippets to solidify your understanding of class structures in C++.
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
- 2 C++
C++ , int, double . ., . , . . , . - , , , . - , . , , . , 2
C++ class _ { // ( ) }; 3
C++ class Person { }; int main() { Person person; } 4
C++ #include <iostream> class Person { public: std::string name; int age; void print() { std::cout << "Name: " << name << "\tAge: " << age << std::endl; } }; int main() { Person person; // person.name = Ivan"; person.age = 42; // person.print(); } 5
#include <iostream> class Person { public: std::string name; int age; void print() { std::cout << "Name: " << name << "\tAge: " << age << std::endl; } Person(std::string p_name, unsigned p_age) { name = p_name; age = p_age; std::cout << "Person has been created" << std::endl; } }; int main() { Person ivan( Ivan", 42); // - ivan.print(); Person anya( Anya", 38); anya.print(); } 6
#include <iostream> class Person { std::string name{}; int age{}; public: void print() { std::cout << "Name: " << name << "\tAge: " << age << std::endl; } Person(std::string p_name, unsigned p_age) { name = p_name; age = p_age; } Person(std::string p_name) { name = p_name; age = 18; } Person() {name = "Unknown"; age = 18; } }; int main() { Person ivan{ Ivan", 42 }; // Person(std::string p_name, unsigned p_age) Person anya{ Anya" }; // Person(std::string p_name) Person ira; // Person() } 7
#include <iostream> class Person { std::string name; int age; public: // Person(std::string p_name = "Unknown", unsigned p_age = 18) { name = p_name; age = p_age; } void print() { std::cout << "Name: " << name << "\tAge: " << age << std::endl; } }; 8
#include <iostream> class Person { public: Person(std::string p_name) { name = p_name; count++; std::cout << "Person " << name << " created. Count: " << count << std::endl; } ~Person() { count--; std::cout << "Person " << name << " deleted. Count: " << count << std::endl; } private: std::string name; static inline unsigned int count = 0; // }; int main() { { Person ivan{ "Ivan" }; Person anya{ "Anya" }; } // Tom Bob Person sam{ "Sam" }; } // Sam 9
#include <iostream> class Person { public: void print() const { std::cout << "Name: " << name << "\tAge: " << age << std::endl; } std::string name; // unsigned age; // }; class Employee : public Person { public: std::string company; // }; int main() { Person ivan; ivan.name = "Ivan"; ivan.age = 42; ivan.print(); // Name: Ivan Age: 42 Employee anya; anya.name = "Anya"; anya.age = 38; anya.company = "Microsoft"; anya.print(); // Name: Anya Age: 38 } 10
#include <iostream> class Camera { // public: void makePhoto() { std::cout << "making a photo" << std::endl; } }; class Phone { // public: void makeCall() { std::cout << "making a call" << std::endl; } }; class Smartphone : public Phone, public Camera { }; // int main() { Smartphone smartphone; smartphone.makePhoto(); // making a photo smartphone.makeCall(); // making a call } 11
. . (public) (public) #include <iostream> class Person { public: std::string name; int age; void print() { std::cout << "Name: " << name << "\tAge: " << age << std::endl; } Person(std::string p_name, unsigned p_age) { name = p_name; age = p_age; } }; int main() { Person ivan{ Ivan", 42 }; // name, age print ivan.name = Anya"; ivan.age = 38; ivan.print(); // Name: Anya Age: 38 } 12
. . (private) (private) #include <iostream> class Person { private: std::string name; int age; public: void print() { std::cout << "Name: " << name << "\tAge: " << age << std::endl; } Person(std::string p_name, unsigned p_age) { name = p_name; age = p_age; } }; int main() { Person ivan{ Ivan", 42 }; // print ivan.print(); // Name: Ivan Age: 42 // name age // ivan.name = Anya"; // ivan.age = -1000; } 13
. . (protected) (protected) #include <iostream> class Person { public: Person(std::string name, unsigned age) { this->name = name; this->age = age; } void print() const { std::cout << "Name: " << name << "\tAge: " << age << std::endl; } protected: std::string name; // private: unsigned int age; }; class Employee : public Person { public: Employee(std::string name, unsigned age, std::string company) : Person(name, age) { this->company = company; } void printEmployee() const { std::cout << name << " works in " << company << std::endl; } private: std::string company; // }; 14
#include <iostream> class Person { private: std::string name; unsigned age; public: Person(std::string p_name, unsigned p_age) { name = p_name; if (p_age > 0 && p_age < 110) age = p_age; else age = 18; // , } void print() { std::cout << "Name: " << name << "\tAge: " << age << std::endl; } void setAge(unsigned p_age) { if (p_age > 0 && p_age < 110) age = p_age; } std::string getName() { return name; } unsigned getAge() { return age; } }; 15
#include <iostream> class Person { private: std::string name; unsigned age; public: Person(std::string p_name, unsigned p_age); Person(std::string p_name); void print(); }; 16
// Person::Person(std::string p_name, unsigned p_age) { name = p_name; age = p_age; } // Person::Person(std::string p_name) : Person(p_name, 18) { } void Person::print() { std::cout << "Name: " << name << "\tAge: " << age << std::endl; } 17
this this #include <iostream> class Point { public: Point(int x, int y) { this->x = x; this->y = y; } void showCoords() { std::cout << "Point x: " << this->x << "\t y: " << y << std::endl; } private: int x; int y; }; int main() { Point p1{ 20, 50 }; p1.showCoords(); } 18
#include <iostream> class Person { public: Person(std::string p_name, unsigned p_age) { ++count; // name = p_name; age = p_age; } void print_count() { std::cout << "Created " << count << " objects" << std::endl; } private: std::string name; unsigned age; static inline unsigned count{}; // - Person }; int main() { Person ivan{ "Ivan", 42 }; Person anya{ "Anya", 38 }; Person sam{ "Sam", 25 }; ivan.print_count(); anya.print_count(); sam.print_count(); } 19
#include <iostream> class Person { public: static inline unsigned int maxAge = 100; // Person(std::string p_name, unsigned p_age) { count++; name = p_name; if (p_age < maxAge) { age = p_age; } } static void print_count() { std::cout << "Created " << count << " objects" << std::endl; } // private: std::string name; unsigned int age = 18; static inline unsigned int count = 0; // }; int main() { Person ivan{ "Ivan", 42 }; Person anya{ "Anya", 38 }; Person sam{ "Sam", 25 }; Person::print_count(); // print_count std::cout << "Max age: " << Person::maxAge << std::endl; // maxAge Person::maxAge = 110; // maxAge std::cout << "Max age: " << Person::maxAge << std::endl; } 20
#include <iostream> class Person { public: Person(std::string name) : name{ name } { } virtual void print() const { // std::cout << "Name: " << name << std::endl; } private: std::string name; }; class Employee : public Person { public: Employee(std::string name, std::string company) : Person{ name }, company{ company } { } void print() const override { // , Person::print(); std::cout << "Works in " << company << std::endl; } private: std::string company; }; 21
- 2 C++