鱼C论坛

 找回密码
 立即注册
查看: 2678|回复: 7

程序员女妹子求助!

[复制链接]
发表于 2014-5-3 15:27:02 | 显示全部楼层 |阅读模式
1鱼币
关于友元函数的一个程序,编译不成功,不知道哪儿的问题
#include<iostream>
using namespace std ;
class Data ;//对Date类的声明
class Time
{
public:
        Time(int , int , int) ;
        friend void display(const Date & , const Time &);//将普通函数display声明为朋友
private:
        int hour ;
        int minute ;
        int sec ;
};
Time::Time(int h , int m , int s)
{
        hour = h ;
        minute = m ;
        sec = s ;
}
class Date
{
public:
        Date (int , int , int);
        friend void display(const Date & , const Time &);//将普通函数display声明为朋友
private:
        int month ;
        int day ;
        int year ;
};
Date::Date(int m , int d , int y )
{
        month = m ;
        day = d ;
        year = y ;
}
void dispaly(const Date &d , const Time &t)
{
        cout << d.month << "/" << d.day << "/" << d.year << endl ;
        cout << t.hour << ":" << t.minute << ":" << t.sec << endl ;
}
int main()
{
        Date t1(12 , 25 , 2014) ;
        Time t2 (10 , 13 , 56);
        display(t1 , t2);
        return 0 ;
}


最佳答案

查看完整内容

楼主该打!这单词。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-3 15:27:03 | 显示全部楼层
  1. #include<iostream>
  2. using namespace std;

  3. //class Data ;//对Date类的声明
  4.   class Date ;//对Date类的声明

  5. class Time
  6. {
  7. public:
  8.         Time(int h , int m , int s);
  9.         friend void display(const Date &, const Time &);//将普通函数display声明为朋友

  10. private:
  11.         int hour ;
  12.         int minute ;
  13.         int sec ;
  14. };
  15. Time::Time(int h , int m , int s)
  16. {
  17.         hour = h ;
  18.         minute = m ;
  19.         sec = s ;
  20. }

  21. class Date
  22. {
  23. public:
  24.         Date (int , int , int);
  25.         friend void display(const Date &, const Time &);//将普通函数display声明为朋友

  26. private:
  27.         int month ;
  28.         int day ;
  29.         int year ;
  30. };
  31. Date::Date(int m , int d , int y )
  32. {
  33.         month = m ;
  34.         day = d ;
  35.         year = y ;
  36. }

  37. //void dispaly(const Date &d, const Time &t)
  38.   void display(const Date &d, const Time &t)
  39. {
  40.         cout << d.month << "/" << d.day << "/" << d.year << endl ;
  41.         cout << t.hour << ":" << t.minute << ":" << t.sec << endl ;
  42. }

  43. int main()
  44. {
  45.         Date t1(12 , 25 , 2014) ;
  46.         Time t2 (10 , 13 , 56);
  47.        
  48.         display(t1 , t2);
  49.        
  50.         return 0 ;
  51. }
复制代码
楼主该打!这单词。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-3 15:53:01 | 显示全部楼层
虽然我看不懂但是你可以从反编译那边寻找答案
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-3 17:39:18 | 显示全部楼层
  1. #include <iostream>
  2. #include <string>
  3. #include <stdlib.h>

  4. class Lovers                                 //创建基类和子类
  5. {

  6. public:
  7.         Lovers(std::string theName);           //基类构造器
  8.         void kiss(Lovers *lover);
  9.         void ask(Lovers *lover, std::string something);
  10. protected:
  11.         std::string name;

  12.         friend class Others;
  13. };

  14. class Boyfriend : public Lovers                  //创建子类并继承基类
  15. {
  16. public:
  17.         Boyfriend(std::string theName);           //子类构造器

  18. };

  19. class Girlfriend : public Lovers
  20. {
  21. public:
  22.         Girlfriend(std::string tneName);

  23. };

  24. class Others
  25. {
  26. public:
  27.                 Others(std::string theName);

  28.             void kiss(Lovers *lover);

  29. protected:
  30.         std::string name;
  31. };

  32. /**************************************/

  33. Lovers::Lovers(std::string theName)                //基类构造器和方法的定义
  34. {
  35.         name = theName;
  36. }

  37. void Lovers::kiss(Lovers *lover)             //对基类函数的定义
  38. {
  39.         std::cout << name << "亲亲我的宝贝" << lover ->name << std::endl;
  40. }

  41. void Lovers::ask(Lovers *lover, std::string something)               //基类函数
  42. {
  43.         std::cout << "宝贝" << lover->name << "和我" << something << std::endl;
  44. }

  45. Boyfriend::Boyfriend(std::string theName) : Lovers(theName)         //对子类构造器的定义继承
  46. {
  47. }

  48. Girlfriend::Girlfriend(std::string theName) : Lovers(theName)         //子类构造器
  49. {
  50. }

  51. Others::Others(std::string theName)
  52. {
  53.         name = theName;
  54. }

  55. void Others::kiss(Lovers *lover)
  56. {
  57.         std::cout << name << "亲一下" << lover->name << std::endl;
  58. }

  59. /********************************************/

  60. int main()
  61. {
  62.         Boyfriend boyfriend("小妞");
  63.         Girlfriend girlfriend("靓妹");

  64.         Others others("帅哥");

  65.         girlfriend.kiss(&boyfriend);
  66.         girlfriend.ask(&boyfriend, "上床睡觉啦");

  67.         std::cout << "\n当当当当!传说中的帅哥登场........\n";
  68.         others.kiss(&girlfriend);
  69.        
  70.         return 0;

  71.         system("pause");

  72. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-5-4 13:42:10 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-5-4 13:44:11 | 显示全部楼层
HHR 发表于 2014-5-3 16:04
楼主该打!这单词。。。

你精神上已经打击到我了。。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2014-5-4 13:45:26 | 显示全部楼层

亲爱的,你很有才哦,不过貌似不是我的答案耶
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2014-5-4 17:36:34 | 显示全部楼层
qq小小七 发表于 2014-5-4 13:45
亲爱的,你很有才哦,不过貌似不是我的答案耶

额!过奖,过奖!我只是把小甲鱼的代码贴出来而已,想让你借鉴的看一下,呵呵!其实,我和你一样,很菜的!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-9 18:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表