列表

详情


CPP42. 友元全局函数

描述

在现有代码的基础上,使用友元全局函数,让程序能够正常运行。

输入描述

输出描述

输出年龄

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++ 解法, 执行用时: 2ms, 内存消耗: 272KB, 提交时间: 2021-11-13

#include <iostream>
using namespace std;

class Person {
    // write your code here......
    friend void showAge(Person & p);

    public:
        Person(int age) {
            this->age = age;
        }

    private:
        int age;
};

void showAge(Person& p) {
    cout << p.age << endl;
}

int main() {

    Person p(10);
    showAge(p);

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 276KB, 提交时间: 2022-05-30

#include <iostream>
using namespace std;
 
class Person {
    // write your code here......
    friend void showAge(Person& p);
     
    public:
        Person(int age) {
            this->age = age;
        }
 
    private:
        int age;
};
 
void showAge(Person& p) {
    cout << p.age << endl;
}
 
int main() {
 
    Person p(10);
    showAge(p);
 
    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 280KB, 提交时间: 2022-01-22

#include <iostream>
using namespace std;

class Person {
    // write your code here......
    friend void showAge(Person);//

    public:
        Person(int age) {
            this->age = age;
        }

    private:
        int age;
};

void showAge(Person p) {
    cout << p.age << endl;
}

int main() {

    Person p(10);
    showAge(p);

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 280KB, 提交时间: 2022-01-04

#include <iostream>
using namespace std;

class Person {
    // write your code here......
    friend void showAge(Person &);  //这样啊

    public:
        Person(int age) {
            this->age = age;
        }

    private:
        int age;
};

void showAge(Person& p) {
    cout << p.age << endl;
}

int main() {

    Person p(10);
    showAge(p);

    return 0;
}

C++ 解法, 执行用时: 2ms, 内存消耗: 280KB, 提交时间: 2021-11-10

#include <iostream>
using namespace std;

class Person {
    // write your code here......
    
    friend void showAge(Person &);
    public:
        Person(int age) {
            this->age = age;
        }

    private:
        int age;
};

void showAge(Person& p) {
    cout << p.age << endl;
}

int main() {

    Person p(10);
    showAge(p);

    return 0;
}

上一题