#include<iostream>
#include<string>
using namespace std;
class Point
{
public:
int x1=2;
int y1=2;
};
class Square :private Point
{
public:
int x2=4;
int y2=4;
int k=0;
int Sm()
{
k = (x2 - x1) * (y2 - y1);
return k;
cout << "k的值为" << k << endl;
}
};
int main()
{
Square s1;
s1.Sm();
}
我用的是VS2019的,然后编译没错,但输出没结果。
class Square :private Point
{
public:
int x2=4;
int y2=4;
int k=0;
int Sm()
{
k = (x2 - x1) * (y2 - y1);
return k;
cout << "k的值为" << k << endl;
}
};
里Sm()函数的语句顺序的问题。
应该把return k;写在cout << "k的值为" << k << endl;后面。
执行return会回到调用这个函数的地方,后面的语句不再执行。