c++类对象作为成员

c++类对象作为成员

#include <iostream>
#include <string>
using namespace std;

class A
{
public:
    int age;
    string name;

public:
    A(int a, string b);
    ~A();
};

A::A(int a, string b)
{
    this->age = a;
    this->name = b;
}

A::~A()
{
}

class B
{
private:
    int age_b;
    string name_b;

public:
    B(int k, string b, A c);
    ~B();

    A a;
};

B::B(int k, string b, A c) : age_b(k), name_b(b), a(c)
{
    printf("%d %s %d %s\n", age_b, name_b.c_str(), a.age, a.name.c_str());
}
B::~B()
{
}

void test01()
{

    B b1(10,"zhan三",A(10,"zhan三"));
}
int main(int argc, char const *argv[])
{
    test01();
    return 0;
}

 

posted @ 2026-06-27 13:38  小小仓鼠  阅读(3)  评论(0)    收藏  举报