自考书上的c++困惑,求大佬给答疑一下谢谢!
#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
源代码中继承类函数对于基类数据应该也是有影响的为什么输出结果并没有影响,求大佬解答! 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对象中的值。 来个大神呗,脑瓜子嗡嗡的 $ g++ -g -Wall -o main main.cpp
main.cpp:15:18: error: empty character constant
15 | cout<<"v1="<<V1<<''\tv2=''<<V2;
| ^~
main.cpp:15:20: error: stray ‘\’ in program
15 | cout<<"v1="<<V1<<''\tv2=''<<V2;
| ^
main.cpp:15:25: error: empty character constant
15 | cout<<"v1="<<V1<<''\tv2=''<<V2;
| ^~
main.cpp:41:7: error: extended character “ is not valid in an identifier
41 | cout<<▒▒▒初始化时的随机值:”<<endl;
| ^
main.cpp:41:7: error: extended character ” is not valid in an identifier
main.cpp:47:7: error: extended character “ is not valid in an identifier
47 | cout<<▒▒▒修改基类中的值后:”<<endl;
| ^
main.cpp:47:7: error: extended character ” is not valid in an identifier
main.cpp:55:7: error: extended character “ is not valid in an identifier
55 | cout<<▒▒▒从派生类修改从基类继承的值及本类的值:”<<endl;
| ^
main.cpp:55:7: error: extended character ” is not valid in an identifier
main.cpp:11:6: error: variable or field ‘print’ declared void
11 | void print Value();
| ^~~~~
main.cpp:11:6: error: expected ‘;’ at end of member declaration
11 | void print Value();
| ^~~~~
| ;
main.cpp: In member function ‘void BaseClass::SetValue(int, int)’:
main.cpp:9:1: error: ‘v2’ was not declared in this scope; did you mean ‘V2’?
9 | v2=n;
| ^~
| V2
main.cpp: At global scope:
main.cpp:13:15: error: expected initializer before ‘:’ token
13 | void BaseClass:public BaseClass
| ^
main.cpp:20:27: error: ‘in’ has not been declared
20 | void SetValue(int m,int n,in k)
| ^~
main.cpp: In member function ‘void DerivedClass::PrintValue()’:
main.cpp:30:12: error: ‘print’ is not a member of ‘BaseClass’
30 | BaseClass::print Value();
| ^~~~~
main.cpp: At global scope:
main.cpp:37:11: error: expected primary-expression before ‘baseCla’
37 | BaseClass baseCla;
| ^~~~~~~
main.cpp:37:11: error: expected ‘}’ before ‘baseCla’
main.cpp:35:1: note: to match this ‘{’
35 | {
| ^
main.cpp:41:1: error: ‘cout’ does not name a type
41 | cout<<“初始化时的随机值:”<<endl;
| ^~~~
main.cpp:43:1: error: ‘baseCla’ does not name a type; did you mean ‘BaseClass’?
43 | baseCla.PrintValue();
| ^~~~~~~
| BaseClass
main.cpp:45:1: error: ‘derivedCla’ does not name a type
45 | derivedCla.PrintValue();
| ^~~~~~~~~~
main.cpp:47:1: error: ‘cout’ does not name a type
47 | cout<<“修改基类中的值后:”<<endl;
| ^~~~
main.cpp:49:1: error: ‘baseCla’ does not name a type; did you mean ‘BaseClass’?
49 | baseCla.SetValue(10,20);
| ^~~~~~~
| BaseClass
main.cpp:51:1: error: ‘baseCla’ does not name a type; did you mean ‘BaseClass’?
51 | baseCla.PrintValue();
| ^~~~~~~
| BaseClass
main.cpp:53:1: error: ‘derivedCla’ does not name a type
53 | derivedCla.PrintValue();
| ^~~~~~~~~~
main.cpp:55:1: error: ‘cout’ does not name a type
55 | cout<<“从派生类修改从基类继承的值及本类的值:”<<endl;
| ^~~~
main.cpp:57:1: error: ‘derivedCla’ does not name a type
57 | derivedCla.SetValue(100,200,300);
| ^~~~~~~~~~
main.cpp:59:1: error: ‘baseCla’ does not name a type; did you mean ‘BaseClass’?
59 | baseCla.PrintValue();
| ^~~~~~~
| BaseClass
main.cpp:61:1: error: ‘derivedCla’ does not name a type
61 | derivedCla.PrintValue();
| ^~~~~~~~~~
main.cpp:63:1: error: expected unqualified-id before ‘return’
63 | return 0;
| ^~~~~~
main.cpp:64:1: error: expected declaration before ‘}’ token
64 | }
| ^
$
一大堆错误 原来是这样感谢大佬的解答
页:
[1]