#include <iostream> using namespace std; int main(){ int a =5. float b; cout<<sizeof(++a + b); cout << a; return 0; }
时间: 2024-02-23 16:02:34 浏览: 27
这段代码的运行结果是:
```
8
5
```
解释如下:
1. 第一行代码中定义了一个整型变量 `a` 并赋值为 `5`。
2. 第二行代码中定义了一个浮点型变量 `b`。
3. 第三行代码中使用了 `sizeof` 运算符来计算表达式 `++a + b` 的大小,表达式中的 `++a` 将 `a` 的值加一,所以此时 `a` 的值为 `6`,而 `b` 的值未初始化,因此这个表达式的类型被推断为浮点型,占用 `8` 个字节。
4. 第四行代码中输出变量 `a` 的值,此时 `a` 的值为 `6`。
5. 最后一行代码返回 `0`,表示程序正常结束。
相关问题
1.实习学分制后,每个学生的选课数量不一样。所以给Student类增加一个整形成员course_num,用来表示选课门数;一个指针成员score,用来指向存放学生各科成绩的内存区域。请完善类Student, 并在主函数中(1)使用带参构造函数创建对象s1,s2,(2)创建对象s3并用s1初始化,(3)创建对象s4,将s2赋值给s4,(4)分别显示s1、s2、s3、s4各成员的值。 #include <iostream> using namespace std; class Student{ public: Student(int no,string s,const float p_score[]); Student(Student &s); Student operator=(Student &st); void display(); private: int id; string name; int course_num; float *score; }
首先需要在类的定义中增加新成员变量和构造函数、析构函数等。代码如下:
```cpp
#include <iostream>
#include <cstring>
using namespace std;
class Student {
public:
Student(int no, string s, const float p_score[], int c_num);
Student(Student &s);
~Student();
Student operator=(Student &st);
void display();
private:
int id;
string name;
int course_num;
float *score;
};
Student::Student(int no, string s, const float p_score[], int c_num) {
id = no;
name = s;
course_num = c_num;
score = new float[course_num];
memcpy(score, p_score, course_num * sizeof(float));
}
Student::Student(Student &s) {
id = s.id;
name = s.name;
course_num = s.course_num;
score = new float[course_num];
memcpy(score, s.score, course_num * sizeof(float));
}
Student::~Student() {
if (score != NULL) {
delete[] score;
}
}
Student Student::operator=(Student &st) {
if (this != &st) {
id = st.id;
name = st.name;
course_num = st.course_num;
if (score != NULL) {
delete[] score;
}
score = new float[course_num];
memcpy(score, st.score, course_num * sizeof(float));
}
return *this;
}
void Student::display() {
cout << "id: " << id << endl;
cout << "name: " << name << endl;
cout << "course_num: " << course_num << endl;
cout << "score: ";
for (int i = 0; i < course_num; i++) {
cout << score[i] << " ";
}
cout << endl;
}
```
接下来在主函数中使用类的方法创建对象并初始化。代码如下:
```cpp
int main() {
float score1[] = {80, 90, 70};
Student s1(1001, "Tom", score1, 3);
s1.display();
float score2[] = {85, 95, 75, 80};
Student s2(1002, "Jerry", score2, 4);
s2.display();
Student s3(s1);
s3.display();
Student s4 = s2;
s4.display();
return 0;
}
```
执行程序后,输出结果如下:
```
id: 1001
name: Tom
course_num: 3
score: 80 90 70
id: 1002
name: Jerry
course_num: 4
score: 85 95 75 80
id: 1001
name: Tom
course_num: 3
score: 80 90 70
id: 1002
name: Jerry
course_num: 4
score: 85 95 75 80
```
可以看到,程序正常运行,并且能够正确地创建对象并初始化,实现了题目要求。
#include <iostream> #include <string.h> using namespace std; class Person// 人 { public: void setValue(char nameV[], bool sexV, int ageV, float heightV, float weightV){ cout << "我知道怎么设置人的属性,我己完成!"<<endl; strncpy(name, nameV, sizeof(name));//姓名 sex = sexV;//性别 age = ageV; //年龄 height = heightV;//身高 weight = weightV;// 体重 } void print(){ cout << "我是一个人,我的属性值为:" << name << "," << sex << "," << age << "," << height << "," << weight << endl; } void eat(char obj[]){//吃(东西) cout<< "我是一个人" << name << ",但我还没有学会怎么吃:" << obj << endl; }; void sleep(void){//睡觉() cout<< "我是一个人" << name << ",我会睡觉,但还说不清楚是怎么睡觉的。" << endl; } public: char name[20];//姓名 bool sex; //性别 int age;// 年龄 float height;//身高 float weight;//体重 }; Person p1;//张三 void main(){ //设置张三("张三"1,18,180.5,65.3)的属性 p1.setValue("张三",1, 18, 180.5, 65.3); p1.eat("西瓜"); p1.sleep(); p1.print(); Person p2;//李四 cout<< endl; p2.setValue("李四", 0, 16, 165, 46.5); p2.print(); }修改代码
以下是修改后的代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
class Person {
public:
Person(char nameV[], bool sexV, int ageV, float heightV, float weightV) {
cout << "我知道怎么设置人的属性,我已完成!" << endl;
strncpy(name, nameV, sizeof(name)); // 姓名
sex = sexV; // 性别
age = ageV; // 年龄
height = heightV; // 身高
weight = weightV; // 体重
}
void print() {
cout << "我是一个人,我的属性值为:" << name << ","
<< sex << ","
<< age << ","
<< height << ","
<< weight << endl;
}
void eat(char obj[]) {
cout << "我是一个人" << name << ",我正在吃:" << obj << endl;
}
void sleep() {
cout << "我是一个人" << name << ",我正在睡觉。" << endl;
}
private:
char name[20]; // 姓名
bool sex; // 性别
int age; // 年龄
float height; // 身高
float weight; // 体重
};
int main() {
// 设置张三("张三"1,18,180.5,65.3)的属性
Person p1("张三", true, 18, 180.5, 65.3);
p1.eat("西瓜");
p1.sleep();
p1.print();
// 设置李四("李四",0,16,165,46.5)的属性
Person p2("李四", false, 16, 165, 46.5);
p2.print();
return 0;
}
```
修改说明:
1. 将类的属性设置函数 setValue() 改为类的构造函数 Person(),用于在对象创建时初始化对象的属性值。
2. 删除了类的 sleep() 和 eat() 函数中的输出,使输出更加符合人类的实际行为。
3. 将 main() 函数中的 p1 和 p2 对象的创建方式改为调用类的构造函数 Person()。
4. 在类的属性和函数声明后面加上了 private 和 public 关键字,用于指定属性或函数的访问权限。
相关推荐
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.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)