类体内定义成员函数
#include <iostream> using namespace std;
class Time { public: void set_time();
void show_time();
private: //成员改为公用的 int hour;
int minute;
int sec;
};
void Time::set_time() //在main函数之前定义 { cin >> hour;
cin >> minute;
cin >> sec;
} void Time::show_time() //在main函数之前定义 { cout << hour << ":" << minute << ":" << sec << endl;
} int main() {
Time t1;
t1.set_time();
t1.show_time();
return ;
} : 2.3 改写2.:类体内声明成员函数,类外定义成员函数
#include <iostream> using namespace std;
class Time {
public: void set_time(void) {
cin >> hour;
cin >> minute;
cin >> sec;
} void show_time(void) {
cout << hour << ":" << minute << ":" << sec << endl;
} private: int hour;
int minute;
int sec;
};
c++面向对象程序设计 答案 Time t;
int main() { t.set_time();
t.show_time();
return ;
} c++面向对象程序设计 谭浩强 : #include <iostream> using namespace std;
class Time {
public: void set_time(void);
void show_time(void);
private: int hour;
int minute;
int sec;
};
c++面向对象程序设计 谭浩强 void Time::set_time(void) {
cin >> hour;
cin >> minute;
cin >> sec;
} void Time::show_time(void)
{
cout << hour << ":" << minute << ":" << sec << endl;
} Time t;
int main() {
t.set_time();
t.show_time();
return ;
} : //xt2-4-1.cpp(main.cpp) #include <iostream> using namespace std;
#include "xt2-4.h" int main() {
Student stud;
stud.set_value();
stud.display();
return ;
} //xt2-4-2.cpp(即student.cpp) #include "xt2-4.h" //在此文件中进行函数的定义 #include <iostream> using namespace std; //不要漏写此行 void Student::display() {
cout << "num:" << num << endl;
cout << "name:" << name << endl;
cout << "sex:" << sex << endl;
} void Student::set_value() {
cin >> num;
cin >> name;
cin >> sex;
} : //xt2-5-1.cpp(file1.cpp) #include <iostream> #include "xt2-5.h" int main() {
Array_max arrmax;
arrmax.set_value();
arrmax.max_value();
arrmax.show_value();
return ;
} //xt2-5-2.cpp(arraymax.cpp) #include <iostream> using namespace std;
#include "xt2-5.h" void Array_max::set_value() {
int i;
for (i = ; i < ; i++) cin >> array[i];
} void Array_max::max_value() {
int i;
max = array[];
for (i = ; i<; i++) if (array[i]>max)
max = array[i];
} void Array_max::show_value() {
cout << "max=" << max << endl;
} :解法一 #include <iostream> using namespace std;
class Box {
public: void get_value();
float volume();
void display();
public: float lengh;
float width;
float height;
}; void Box::get_value() {
cout << "please input lengh, width,height:";
cin >> lengh;
cin >> width;
cin >> height;
} float Box::volume() {
return(lengh*width*height);
} void Box::display() {
cout << volume() << endl;
} int main() {
Box box1, box2, box3;
box1.get_value();
cout << "volmue of bax1 is ";
box1.display();
box2.get_value();
cout << "volmue of bax2 is ";
box2.display();
box3.get_value();
cout << "volmue of bax3 is ";
box3.display();
return ;
} 解法二: #include <iostream> using namespace std;
class Box {
public: void get_value();
void volume();
void display();
public: float lengh;
float width;
float height;
float vol;
}; void Box::get_value() {
cout << "please input lengh, width,height:";
cin >> lengh;
cin >> width;
cin >> height;
} void Box::volume() {
vol = lengh*width*height;
} void Box::display() {
cout << vol << endl;
} int main() {
Box box1, box2, box3;
box1.get_value();
box1.volume();
cout << "volmue of bax1 is ";
box1.display();
box2.get_value();
box2.volume();
cout << "volmue of bax2 is ";
box2.display();
box3.get_value();
box3.volume();
cout << "volmue of bax3 is ";
box3.display();
return ;
}

c++面向对象程序设计 谭浩强 第二章答案的更多相关文章

  1. c++面向对象程序设计 谭浩强 第一章答案

    c++面向对象程序设计 谭浩强 答案 第一章 目录: c++面向对象程序设计 谭浩强 答案 第一章 c++面向对象程序设计 谭浩强 答案 第二章 c++面向对象程序设计 谭浩强 答案 第三章 c++面 ...

  2. c++面向对象程序设计 谭浩强 第三章答案

    2: #include <iostream> using namespace std; class Date {public: Date(int,int,int); Date(int,in ...

  3. c++面向对象程序设计 谭浩强 第五章答案

    1: #include <iostream> using namespace std; class Student {public: void get_value() {cin>&g ...

  4. C语言程序设计·谭浩强(第四版)第二章课后习题的答案,算法——程序的灵魂

    C语言程序小练习 1.用C语言设计程序算出1-1/2+1/3-14+1/5...+1/99-1/100的值 #include<stdio.h> int main() { ; double ...

  5. 关于指针的笔记【1】【C语言程序设计-谭浩强】

    指针是什么? 一个 变量的地址称为该变量的"指针"[将地址形象化的称为“指针”].(指针是什么百度百科) 注意区分储存单元的地址和内容这两个概念的区别. 直接访问:直接按变量名进行 ...

  6. c++面向对象程序设计 课后题 答案 谭浩强 第四章

    c++面向对象程序设计课后题答案 谭浩强 第四章 1: #include <iostream> using namespace std; class Complex {public: Co ...

  7. C程序设计(谭浩强)第五版课后题答案 第一章

    大家好,这篇文章分享了C程序设计(谭浩强)第五版课后题答案,所有程序已经测试能够正常运行,如果小伙伴发现有错误的的地方,欢迎留言告诉我,我会及时改正!感谢大家的观看!!! 1.什么是程序?什么是程序设 ...

  8. 再论谭浩强《C语言程序设计》

    一些同学学不好C语言,把罪责归于“因为教材是谭浩强写的”实在是很滑稽. 谭浩强老先生 1934 年生,现在已经 80 岁了.他 1958 年从清华大学自动控制系毕业,那年 24 岁.要知道 C 语言那 ...

  9. 挂羊头卖狗肉蓄意欺骗读者——谭浩强《C程序设计(第四版)》中所谓的“按照C99”(二)

    挂羊头卖狗肉蓄意欺骗读者——谭浩强<C程序设计(第四版)>中所谓的“按照C99”(二) 在<谭C>p4:“本书的叙述以C99标准为依据”,下面从C89到C99的主要变化方面来看 ...

随机推荐

  1. jTemplates的教程,包括{#if}{#foreach}{#for}的简单使用

    最近在做一些局部刷新的分页工作,一般不使用既成的插件的话,就是在脚本里面重新渲染一个局部的html,把需要局部分页的模块重写一遍,还需要在控制器里再定义一个方法返回所需的局部数据,这种做法相当冗余,所 ...

  2. IOS-UITextField-改变光标颜色

    方法1: [[UITextField appearance] setTintColor:[UIColor blackColor]]; 这种方法将影响所有TextField. 方法2: textFiel ...

  3. 用opcity模拟zindex渐变的效果

    github地址: https://github.com/echoorx/opacity-Gradient zindex好像不能渐变改变,所以用opcity来模拟 <!DOCTYPE html& ...

  4. 在webstrorm中配置好es6 babel【更新:在webstorm中配置.vue和.vue文件中支持es6】

    第一步:全局安装babel-cli npm install -g babel-cli 第二步,新建一个空项目,在 WebStorm 中的当前项目中打开 Terminal,进入项目的根目录下, 安装 E ...

  5. SurfaceView加载长图

    1:SurfaceView加载长图,移到.可以充当背景 效果截图 2:View (淡入淡出动画没实现:记录下) package com.guoxw.surfaceviewimage; import a ...

  6. 转/ C# 托管资源和非托管资源

    原文 对于这两个一直就是模模糊糊的,半知零解 托管资源指的是.NET可以自动进行回收的资源,主要是指托管堆上分配的内存资源.托管资源的回收工作是不需要人工干预的,由.NET运行库在合适时调用垃圾回收器 ...

  7. Jquery中的bind()方法的一点问题

    bind()方法绑定事件的时候,第二个参数是函数,如果代码都写在函数里面,没有任何问题.但是,直接调用外部封装的函数需要注意,出错的例子: <!doctype html> <html ...

  8. Problem 7

    Problem 7 # Problem_7.py """ By listing the first six prime numbers: 2, 3, 5, 7, 11, ...

  9. 我的第一个arcgis地图应用

    步骤: 1.设置一个基本的html文档 <!DOCTYPE html> <html> <head> <meta http-equiv="Conten ...

  10. HDU 2857 Mirror and Light

    /* hdu 2857 Mirror and Light 计算几何 镜面反射 */ #include<stdio.h> #include<string.h> #include& ...