Атрибути Класу
syntax.h
9
1
2
3
4
class Name {
public:
Type AttributeName;
};
1234class Name { public: Type AttributeName; };
Публічні атрибути та методи класу можуть бути безпосередньо доступні за допомогою оператора крапки (.
) через об'єкт цього класу.
main.cpp
99
1
2
3
4
5
6
7
8
9
10
11
12
13
class Student {
public:
int age;
};
int main()
{
Student bob;
Student ann;
bob.age = 25;
ann.age = 33;
}
12345678910111213class Student { public: int age; }; int main() { Student bob; Student ann; bob.age = 25; ann.age = 33; }
Завдання
Swipe to start coding
Ви працюєте з класом Student
і маєте вдосконалити його структуру та використання.
- Додайте два додаткових атрибути до класу
Student
:name
(string) таgpa
(float). - Створіть об'єкт класу
Student
. - Присвойте відповідні значення атрибутам
name
,gpa
таemail
створеного екземпляра.
Рішення
solution.cpp
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
class Student {
public:
std::string name;
float gpa;
std::string email;
};
int main()
{
Student student;
student.name = "Bob Wise";
student.gpa = 4.0;
student.email = "great_bob@gmail.com";
}
Все було зрозуміло?
Дякуємо за ваш відгук!
Секція 1. Розділ 4
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
class Student {
public:
___ ___;
___ ___;
std::string email;
};
int main()
{
___ ___;
___ = ___;
___ = ___;
student.email = "great_bob@gmail.com";
}
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат