鱼C论坛

 找回密码
 立即注册
查看: 1967|回复: 5

[已解决]自考书上的c++困惑,求大佬给答疑一下谢谢!

[复制链接]
发表于 2021-7-17 22:08:08 | 显示全部楼层 |阅读模式
20鱼币
#include<iostream>
using namespace std;
class BaseClass
{   int V1,V2;
public:
void SetValue(int m, int n)
{
V1=m;
v2=n;
}
void print Value();
};
void BaseClass:public BaseClass
{
cout<<"v1="<<V1<<''\tv2=''<<V2;
}
class DerivedClass:public BaseClass
{ int V3;
public:
void SetValue(int m,int n,in k)
{
BaseClass::SetValue(m,n);
V3=k;
}
void PrintValue();
};
void DerivedClass::PrintValue()
{
cout<<endl;
BaseClass::print Value();
cout<<"\tV3="<<V3<<endl;
}

int main()
{

BaseClass baseCla;

DerivedClass derivedCla;

cout<<“初始化时的随机值:”<<endl;

baseCla.PrintValue();

derivedCla.PrintValue();

cout<<“修改基类中的值后:”<<endl;

baseCla.SetValue(10,20);

baseCla.PrintValue();

derivedCla.PrintValue();

cout<<“从派生类修改从基类继承的值及本类的值:”<<endl;

derivedCla.SetValue(100,200,300);

baseCla.PrintValue();

derivedCla.PrintValue();

return 0;
}

输出结果:

初始化时的随机值:

V1=-454654654454 V2=-454654654454 //随机值我乱写的
V1=-454654654454 V2=-454654654454 V3=-454654654454

修改基类中的值后:

V1=10 V2=20
V1=-454654654454 V2=-454654654454 V3=-454654654454

从派生类修改从基类继承的值及本类的值:

V1=10 V2=20
V1=100   V2=200   V3=300
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
根据执行结果我有以下几点疑问请大佬解答一下,万分感谢!

cout<<“修改基类中的值后:”<<endl;

baseCla.SetValue(10,20);  //这里通过调用基类成员函数 对基类里的私有成员变量 V1 V2 进行值改变

baseCla.PrintValue();   //这里通过调用基类成员函数 输出结果 V1=10 V2=20

derivedCla.PrintValue(); //这里是通过继承类来调用基类里的输出函数和对本来私有成员变量进行值改变 书上的输出结果是:V1=-454654654454 V2=-454654654454 V3=-454654654454 疑问?不应该是:V1=10 V2=20 V3=-454654654454

从派生类修改从基类继承的值及本类的值:

V1=10 V2=20 //这里输出的结果应该是 V1=100 V2=200 才对,为什么书上是 10 20
V1=100   V2=200   V3=300

源代码中继承类函数对于基类数据应该也是有影响的为什么输出结果并没有影响,求大佬解答!
最佳答案
2021-7-17 22:08:09
derivedCla.PrintValue(); //这里是通过继承类来调用基类里的输出函数和对本来私有成员变量进行值改变 书上的输出结果是:V1=-454654654454 V2=-454654654454 V3=-454654654454 疑问?不应该是:V1=10 V2=20 V3=-454654654454
从派生类修改从基类继承的值及本类的值:
V1=10 V2=20 //这里输出的结果应该是 V1=100 V2=200 才对,为什么书上是 10 20

答:上面两个问题都是同一个原因,代码中 BaseClass baseCla;   DerivedClass derivedCla;  这里定义了两个独立的对象baseCla和derivedCla,两个对象没有关系。因此调用baseCla.SetValue(10,20);并不会改变derivedCla中基类的值。同理调用derivedCla.SetValue(100,200,300);也不会改变baseCla对象中的值。

最佳答案

查看完整内容

derivedCla.PrintValue(); //这里是通过继承类来调用基类里的输出函数和对本来私有成员变量进行值改变 书上的输出结果是:V1=-454654654454 V2=-454654654454 V3=-454654654454 疑问?不应该是:V1=10 V2=20 V3=-454654654454 从派生类修改从基类继承的值及本类的值: V1=10 V2=20 //这里输出的结果应该是 V1=100 V2=200 才对,为什么书上是 10 20 答:上面两个问题都是同一个原因,代码中 BaseClass baseCla; DerivedClass ...
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-17 22:08:09 | 显示全部楼层    本楼为最佳答案   
derivedCla.PrintValue(); //这里是通过继承类来调用基类里的输出函数和对本来私有成员变量进行值改变 书上的输出结果是:V1=-454654654454 V2=-454654654454 V3=-454654654454 疑问?不应该是:V1=10 V2=20 V3=-454654654454
从派生类修改从基类继承的值及本类的值:
V1=10 V2=20 //这里输出的结果应该是 V1=100 V2=200 才对,为什么书上是 10 20

答:上面两个问题都是同一个原因,代码中 BaseClass baseCla;   DerivedClass derivedCla;  这里定义了两个独立的对象baseCla和derivedCla,两个对象没有关系。因此调用baseCla.SetValue(10,20);并不会改变derivedCla中基类的值。同理调用derivedCla.SetValue(100,200,300);也不会改变baseCla对象中的值。
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-17 22:29:26 | 显示全部楼层
来个大神呗,脑瓜子嗡嗡的
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-7-17 22:40:52 | 显示全部楼层
  1. $ g++ -g -Wall -o main main.cpp
  2. main.cpp:15:18: error: empty character constant
  3.    15 | cout<<"v1="<<V1<<''\tv2=''<<V2;
  4.       |                  ^~
  5. main.cpp:15:20: error: stray ‘\’ in program
  6.    15 | cout<<"v1="<<V1<<''\tv2=''<<V2;
  7.       |                    ^
  8. main.cpp:15:25: error: empty character constant
  9.    15 | cout<<"v1="<<V1<<''\tv2=''<<V2;
  10.       |                         ^~
  11. main.cpp:41:7: error: extended character “ is not valid in an identifier
  12.    41 | cout<<&#9618;&#9618;&#9618;初始化时的随机值:”<<endl;
  13.       |       ^
  14. main.cpp:41:7: error: extended character ” is not valid in an identifier
  15. main.cpp:47:7: error: extended character “ is not valid in an identifier
  16.    47 | cout<<&#9618;&#9618;&#9618;修改基类中的值后:”<<endl;
  17.       |       ^
  18. main.cpp:47:7: error: extended character ” is not valid in an identifier
  19. main.cpp:55:7: error: extended character “ is not valid in an identifier
  20.    55 | cout<<&#9618;&#9618;&#9618;从派生类修改从基类继承的值及本类的值:”<<endl;
  21.       |       ^
  22. main.cpp:55:7: error: extended character ” is not valid in an identifier
  23. main.cpp:11:6: error: variable or field ‘print’ declared void
  24.    11 | void print Value();
  25.       |      ^~~~~
  26. main.cpp:11:6: error: expected ‘;’ at end of member declaration
  27.    11 | void print Value();
  28.       |      ^~~~~
  29.       |           ;
  30. main.cpp: In member function ‘void BaseClass::SetValue(int, int)’:
  31. main.cpp:9:1: error: ‘v2’ was not declared in this scope; did you mean ‘V2’?
  32.     9 | v2=n;
  33.       | ^~
  34.       | V2
  35. main.cpp: At global scope:
  36. main.cpp:13:15: error: expected initializer before ‘:’ token
  37.    13 | void BaseClass:public BaseClass
  38.       |               ^
  39. main.cpp:20:27: error: ‘in’ has not been declared
  40.    20 | void SetValue(int m,int n,in k)
  41.       |                           ^~
  42. main.cpp: In member function ‘void DerivedClass::PrintValue()’:
  43. main.cpp:30:12: error: ‘print’ is not a member of ‘BaseClass’
  44.    30 | BaseClass::print Value();
  45.       |            ^~~~~
  46. main.cpp: At global scope:
  47. main.cpp:37:11: error: expected primary-expression before ‘baseCla’
  48.    37 | BaseClass baseCla;
  49.       |           ^~~~~~~
  50. main.cpp:37:11: error: expected ‘}’ before ‘baseCla’
  51. main.cpp:35:1: note: to match this ‘{’
  52.    35 | {
  53.       | ^
  54. main.cpp:41:1: error: ‘cout’ does not name a type
  55.    41 | cout<<“初始化时的随机值:”<<endl;
  56.       | ^~~~
  57. main.cpp:43:1: error: ‘baseCla’ does not name a type; did you mean ‘BaseClass’?
  58.    43 | baseCla.PrintValue();
  59.       | ^~~~~~~
  60.       | BaseClass
  61. main.cpp:45:1: error: ‘derivedCla’ does not name a type
  62.    45 | derivedCla.PrintValue();
  63.       | ^~~~~~~~~~
  64. main.cpp:47:1: error: ‘cout’ does not name a type
  65.    47 | cout<<“修改基类中的值后:”<<endl;
  66.       | ^~~~
  67. main.cpp:49:1: error: ‘baseCla’ does not name a type; did you mean ‘BaseClass’?
  68.    49 | baseCla.SetValue(10,20);
  69.       | ^~~~~~~
  70.       | BaseClass
  71. main.cpp:51:1: error: ‘baseCla’ does not name a type; did you mean ‘BaseClass’?
  72.    51 | baseCla.PrintValue();
  73.       | ^~~~~~~
  74.       | BaseClass
  75. main.cpp:53:1: error: ‘derivedCla’ does not name a type
  76.    53 | derivedCla.PrintValue();
  77.       | ^~~~~~~~~~
  78. main.cpp:55:1: error: ‘cout’ does not name a type
  79.    55 | cout<<“从派生类修改从基类继承的值及本类的值:”<<endl;
  80.       | ^~~~
  81. main.cpp:57:1: error: ‘derivedCla’ does not name a type
  82.    57 | derivedCla.SetValue(100,200,300);
  83.       | ^~~~~~~~~~
  84. main.cpp:59:1: error: ‘baseCla’ does not name a type; did you mean ‘BaseClass’?
  85.    59 | baseCla.PrintValue();
  86.       | ^~~~~~~
  87.       | BaseClass
  88. main.cpp:61:1: error: ‘derivedCla’ does not name a type
  89.    61 | derivedCla.PrintValue();
  90.       | ^~~~~~~~~~
  91. main.cpp:63:1: error: expected unqualified-id before ‘return’
  92.    63 | return 0;
  93.       | ^~~~~~
  94. main.cpp:64:1: error: expected declaration before ‘}’ token
  95.    64 | }
  96.       | ^
  97. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-17 22:57:43 | 显示全部楼层
一大堆错误
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-7-18 19:37:47 | 显示全部楼层
原来是这样感谢大佬的解答
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-4-27 05:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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