پاسخ داده شده: برنامه Struct با زبان c++
سلام؛ ایکاش که مشخص می کردید دقیقاً کدوم قسمت این سوأل رو مشکل دارید. امّا در کلیّت شما می تونید از تعریف یک struct برای ساختمان داده ای که نیاز دارید و یک class برای استفاده از اون ساختمان داده بهره ببرید. مثلاً:
#include <iostream>
#include <string>
struct person
{ std::string name; unsigned short age; person() = default; person(const std::string& n, const unsigned short _a) : name(n), age(_a) { }
};
class student
{
private: person m_person;
public: student() = default; student(const std::string& n, const unsigned short _a) : m_person(n, _a) { } friend std::ostream& operator<<(std::ostream&, const student&); friend std::istream& operator>>(std::istream&, student&);
};
std::ostream& operator<<(std::ostream& out, const student& other)
{ out << "name: " << other.m_person.name << std::endl; out << "age : " << other.m_person.age << std::endl; retu out;
}
std::istream& operator>>(std::istream& in, student& other)
{ std::cout << "Enter name : "; in >> other.m_person.name; std::cout << "Enter age : "; in >> other.m_person.age; retu in;
}
int main()
{ student a; std::cin >> a; std::cout << a;
}
