#include<iostream> using namespace std; class Base { //类中成员默认是private public: Base() { cout<<__LINE__<<":"<<__func__<<endl; } ~Base() { cout<<__LINE__<<":"<<__func__<<endl; } protected: void setval(int val); public: int getval() const; //const修饰的成员函数只能调用const修饰的方法不能调用非const方法,不能修改类的对象,可以修改static成员变量的值 private: int val; static int sval; static char scval; }; char Base::scval='a'; int Base::sval=123; //初始化静态变量 void Base::setval(int val) { this->val=val; } int Base::getval()const { return this->val; } int main() { cout<<"类中的静态变量"<<Base::scval<<"和"<<Base::sval<<endl; Base obj; return 0; }
时间: 2024-03-15 20:41:32 浏览: 70
这段代码与之前相同,实现了一个名为Base的类,其中包含一个默认的构造函数和析构函数,以及一个int类型的私有成员变量val,一个protected类型的成员函数setval用于设置val的值,一个公有的成员函数getval用于获取val的值。此外,还有两个静态成员变量sval和scval,分别为int类型和char类型,这两个变量可以通过类名直接访问。
在main函数中,首先输出了类中的静态变量的值,然后创建了一个Base类的对象obj,最后返回0。
需要注意的是,该代码中使用了C++11的__func__宏用于输出函数名和__LINE__宏用于输出行号。
相关问题
代码改错:#include<iostream> using namespace std; class base { private: int x; public: void setx(int n) { x = n; } void showx() { cout<<x<<endl; } }; class derived:private base { private: int y; public: void setxy(int n, int m) { setx(n); y = m; } void showxy() { cout<<x<<endl; cout<<y<<endl; } } int main() { derived obj; obj.setx(10); obj.showx(); obj.setxy(20, 30); obj.showxy(); return 0; }
代码有一个语法错误,需要在 derived 类的最后加上一个分号。
正确代码如下:
```
#include<iostream>
using namespace std;
class base {
private:
int x;
public:
void setx(int n)
{
x = n;
}
void showx()
{
cout<<x<<endl;
}
};
class derived:private base {
private:
int y;
public:
void setxy(int n, int m)
{
setx(n);
y = m;
}
void showxy()
{
cout<<x<<endl;
cout<<y<<endl;
}
}; // 添加分号
int main() {
derived obj;
obj.setx(10);
obj.showx();
obj.setxy(20, 30);
obj.showxy();
return 0;
}
```
代码改错:#include<iostream> using namespace std; class base { private: int x; public: void setx(int n) { x = n; } void showx() { cout<<x<<endl; } }; class derived:private base { private: int y; public: void setxy(int n, int m) { setx(n); y = m; } void showxy() { cout<<x<<endl; cout<<y<<endl; } } int main() { derived obj; obj.setx(10); obj.showx(); obj.setxy(20, 30); obj.showxy(); return 0; }
代码有一个语法错误,需要在 derived 类的最后加上一个分号。
正确代码如下:
```
#include<iostream>
using namespace std;
class base {
private:
int x;
public:
void setx(int n)
{
x = n;
}
void showx()
{
cout<<x<<endl;
}
};
class derived:private base {
private:
int y;
public:
void setxy(int n, int m)
{
setx(n);
y = m;
}
void showxy()
{
cout<<x<<endl;
cout<<y<<endl;
}
}; // 添加分号
int main() {
derived obj;
obj.setx(10);
obj.showx();
obj.setxy(20, 30);
obj.showxy();
return 0;
}
```
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)