+++++Абстракция данных


++ Создайте класс с именем Студент. Создайте метод определения суммы


Download 57.24 Kb.
bet22/23
Sana20.11.2023
Hajmi57.24 Kb.
#1790263
1   ...   15   16   17   18   19   20   21   22   23
Bog'liq
dasturlash

++ Создайте класс с именем Студент. Создайте метод определения суммы
стипендии на основе его оценки и используйте его в основной программе.
Создайте класс с именем Университет. Объявляйте методы и свой
#include
#include
class Student {
private:
std::string name;
int grade;
public:
Student(std::string studentName, int studentGrade) {
name = studentName;
grade = studentGrade;
}
void displayInfo() {
std::cout << "Name: " << name << std::endl;
std::cout << "Scholarship: " << getScholarship() << std::endl;
}
int getScholarship() {
if (grade >= 90) {
return 1000;
} else if (grade >= 80) {
return 800;
} else if (grade >= 70) {
return 600;
} else {
return 0;
}
}
};
int main() {
Student student("John", 85);
student.displayInfo();
return 0;
}


++ Создание дружественных классов в C++
#include
class School {
private:
std::string name;
int numStudents;
public:
School(std::string name, int numStudents) {
this->name = name;
this->numStudents = numStudents;
}
friend class FriendClass;
};
class FriendClass {
public:
void printSchoolInfo(School school) {
std::cout << "School Name: " << school.name << std::endl;
std::cout << "Number of Students: " << school.numStudents << std::endl;
}
};
int main() {
School school("ABC School", 500);
"FriendClass"
FriendClass friendObj;
friendObj.printSchoolInfo(school);
return 0;
}
++ Создание дружественных классов и методов в C++
#include
class MyClass {
private:
int privateData;
protected:
int protectedData;
public:
MyClass(int privateData, int protectedData) {
this->privateData = privateData;
this->protectedData = protectedData;
}

friend class FriendClass;


};
class FriendClass {
public:
void printData(MyClass obj) {
std::cout << "Private data: " << obj.privateData << std::endl;
std::cout << "Protected data: " << obj.protectedData << std::endl;
}
};
int main() {
MyClass obj(10, 20);
FriendClass friendObj;
friendObj.printData(obj);
return 0;
}

Download 57.24 Kb.

Do'stlaringiz bilan baham:
1   ...   15   16   17   18   19   20   21   22   23




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling