![](https://img-blog.csdnimg.cn/20201014180756928.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c/c++
文章平均质量分 68
培风
C/C++,嵌入式。
展开
-
《高质量C++/C编程指南》笔记——内存管理[3, 指针与数组的对比]
7.3 指针与数组的对比数组要么在静态存储区被创建(如全局数组),要么在栈上被创建。数组明对应着(而不是指向)一块内存,其地址与容量在生命期内保持不变,只有数组的内容可以改变。指针可以随时指向任意类型的内存块,它的特征是“可变”,所以我们常用指针来操作动态内存。指针远比数组灵活,但也更危险。下面以字符串为例比较指针与数组的特性7.3.1 修改内容char a[] = "he原创 2015-10-08 22:58:11 · 457 阅读 · 0 评论 -
日期时间类
#include using namespace std;class Date{public: Date(int y=0,int m=0,int d=0):year(y),month(m),day(d){} void SetDate(int y,int m,int d); void PrintDate();private: int year; int month; int d原创 2014-05-24 20:17:41 · 467 阅读 · 0 评论 -
摩托车继承自行车和机动车
#include #include #include using namespace std;enum vehicleStaus {rest, running}; //车辆状态:泊车、行进class vehicle //车辆类{protected: int maxSpeed; //最大车速 int currentSpeed; //当前速度 int weight; //车原创 2014-05-24 00:03:17 · 938 阅读 · 0 评论 -
教师兼干部类
#include #include using namespace std;class Teacher//教师类{public: Teacher(string na,int ag,char se,string tit,string ad,string num):name(na),age(ag),sex(se),title(tit),address(ad),number(num){}原创 2014-05-21 22:48:00 · 491 阅读 · 0 评论 -
项目2 - 用文件保存的学生名单
/**作者:lws*完成时间:2014/6/5*问题描述:文件操作*/#include #include #include #include #include using namespace std;class Student{public: Student(){} double getCpp() { return cpp; } double getMath原创 2014-06-05 16:00:56 · 582 阅读 · 0 评论 -
项目1-工资数据的输入
/**作者:*完成日期:2014/6/3*问题描述:输入若干职工的工资(1000-10000的数字),将这些数字按降序排序后输出*/#include #include //支持freopen的头文件using namespace std;int main(){ double salarys[500]; int n=0,i=0,j=0,t; freop原创 2014-06-03 20:22:24 · 723 阅读 · 0 评论 -
长颈鹿类对动物类的protected继承
#include using namespace std;class Animal{public: Animal() {} void eat() { cout << "eat\n"; }protected: void play() { cout << "play\n"; }private:原创 2014-05-21 21:43:59 · 477 阅读 · 0 评论 -
项目1-工资数据的输入_续
/**作者:lws*完成日期:2014/6/3*问题描述:输入若干职工的工资(1000-10000的数字),将这些数字按降序排序后输出*实现:写入数据后,按降序排序,输出到文件里*/#include #include #include using namespace std;int main(){ double salarys[500],t; int n=0,原创 2014-06-03 22:19:18 · 542 阅读 · 0 评论 -
长颈鹿类对动物类的private继承
#include using namespace std;class Animal{public: Animal() {} void eat() { cout << "eat\n"; }protected: void play() { cout << "play\n"; }private:原创 2014-05-21 21:35:25 · 489 阅读 · 0 评论 -
动物这样叫(多态性、虚函数和抽象类)
#include #include using namespace std;class Animal{public: Animal(string na):name(na){} virtual void cry()=0;//纯虚函数,使Animal成为抽象类protected: string name;};class Mouse:public Animal{原创 2014-05-26 18:25:45 · 500 阅读 · 0 评论 -
形状类族的中的纯虚函数
#include #include using namespace std;class Shape{public: virtual double area()=0;};class Circle:public Shape{public: Circle(double r):radius(r){} double area() { retu原创 2014-05-26 21:40:02 · 481 阅读 · 0 评论 -
c++习题1_A_长方体继承自矩形
/**作者:lws*完成时间:2014/6/12*输入:长方体的长,宽,高*输出:长方体的体积*/#include using namespace std;class Rectangle //矩形类{public: Rectangle():length(0),width(0){} Rectangle(int l,int w):length(l),width(w)原创 2014-06-12 12:38:37 · 813 阅读 · 0 评论 -
《高质量C++/C编程指南》笔记——内存管理1-2
7.1 内存分配方式内存分配方式有三种:(1)从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static变量。(2)在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集中,效率很高,但是分配的内存容量有限。(3)从堆上分配,亦称动态内存分原创 2015-10-05 23:48:32 · 479 阅读 · 0 评论 -
c++习题1_G_抽象基类
/**作者:lws*完成时间:2014/6/13*描述:通过对Teacher和BirthDate使用继承和组合的方式设计Professor*输入:2001 Huang m 1970 1 1 1994 5 26*输出:num:2001 name:Huang sex:m birthday:1994/5/26*/#include #include #define PI 3.14159原创 2014-06-14 16:08:26 · 641 阅读 · 0 评论 -
c++习题1_H_虚函数-计算图形面积
/**作者:lws*完成时间:2014/6/14*描述:编写一个程序,定义抽象基类Shape,由它派生出5个派生类: Circle(圆形)、Square(正方形)、Rectangle(矩形)、Trapezoid(梯形)、Triangle(三角形)。用虚函数分别计算几种图形面积,并求它们之和。要求用基类指针数组,使它每一个元素指向一个派生类对象。*输入:12.6 3.5 4.5 8.4原创 2014-06-14 16:37:51 · 1733 阅读 · 1 评论 -
c++习题1_F_继承与组合
/**作者:lws*完成时间:2014/6/13*描述:通过对Teacher和BirthDate使用继承和组合的方式设计Professor*输入:2001 Huang m 1970 1 1 1994 5 26*输出:num:2001 name:Huang sex:m birthday:1994/5/26*/#include #include using namespace std原创 2014-06-13 22:37:06 · 674 阅读 · 0 评论 -
c++习题1_B_c++继承改错
/**作者:lws*完成时间:2014/6/12*输入:1 2 3 4 5*输出:a:3 b:4 a:3 b:4 c:5*/#include using namespace std;class A{public: A(int aa,int bb){a=aa,b=bb;} void printA() { cout<<"a:"<<a<<"b:原创 2014-06-12 13:17:41 · 588 阅读 · 0 评论 -
c++习题1_E_多重继承
/**作者:lws*完成时间:2014/6/12*描述:分别声明Teacher(教师)类和Cadre(干部)类,采用多重继承方式由这两个类派生出新类Teacher_Cadre(教师兼干部)类*输入:Wang-li 50 f prof. president 135 Beijing Road,Shanghai (021)61234567 1534.5*输出:name:Wang-li age:原创 2014-06-12 17:55:33 · 805 阅读 · 0 评论 -
c++习题1_D_输入输出--保护继承
/**作者:lws*完成时间:2014/6/12*输入:1001 zhang m 21 shanghai*输出:num: 1001 name: zhang sex: m age: 21 address: shanghai */#include using namespace std;class Student{public: void get_value() {原创 2014-06-12 16:09:51 · 655 阅读 · 0 评论 -
c++习题1_C_复数运算符重载(+与<<)
/**作者:lws*完成时间:2014/6/12*输入:3 4 5 -10 3 4 5 5 3 4*输出:c1+c2=(8.00-6.00i) c1+d=(8.00+4.00i) d+c1=(8.00+4.00i)*/#include #include //#include using namespace std;class Complex{public: Comp原创 2014-06-12 15:40:08 · 855 阅读 · 0 评论 -
长颈鹿类对动物类的public继承
#include using namespace std;class Animal //动物类{public: Animal() {} void eat(){ cout << "eat\n"; }protected: void play() { cout << "play\n"; }private:原创 2014-05-21 20:58:59 · 503 阅读 · 0 评论 -
类族的设计
#include using namespace std;class Point //点类{public: Point():x(0),y(0){} Point(double x0,double y0):x(x0),y(y0){} ~Point(){} friend ostream &operator<<(ostream &out,const Point &p);protected原创 2014-05-20 18:25:24 · 434 阅读 · 0 评论 -
深复制体验
#include using namespace std;class A{private: char *a;public: A(char *aa) { a=new char[strlen(aa)+1]; strcpy(a,aa); } ~A() { delete []a; } A(A &c) { a=new char[strlen(c.a)+1];原创 2014-05-15 22:28:50 · 380 阅读 · 0 评论 -
旱冰场造价
今天开始跟着贺利坚老师的博客原创 2014-05-10 10:13:48 · 506 阅读 · 0 评论 -
再一个深复制
#includeusing namespace std;class A{private: int *arrayAddr;//保存一个有len个整型元素的数组的首地址 int len; //记录动态数组的长度 int max; //动态数组中的最大值(并非动态数组中必须要的数据成员)public: A(int *a, int n);原创 2014-05-15 22:53:41 · 377 阅读 · 0 评论 -
时间类
#include using namespace std;class Time{public: void set_time(); void show_time(); int add_a_sec()//增加1秒钟 { second+=1; return second; } int add_a_minute()//增加1分钟 { minute+=1; return原创 2014-05-10 13:35:00 · 572 阅读 · 0 评论 -
复数类
#include using namespace std;class Complex{public: Complex(){real=0;imag=0;} Complex(double r,double i){real=r;imag=i;} Complex operator+(Complex &c); Complex operator-(Complex &c); Complex o原创 2014-05-17 00:48:00 · 483 阅读 · 0 评论 -
分数类的雏形
#include #include using namespace std;class CFraction{public: CFraction(int nu=0,int de=1); void output(int style=0); void set(int nu=0,int de=1); void input(); void simplify(); void amplif原创 2014-05-11 00:30:19 · 407 阅读 · 0 评论 -
对象作为数据成员
#include #include using namespace std;class Cpoint{public: Cpoint(double xx=0,double yy=0):x(xx),y(yy){}; double Distancel(Cpoint p) const; //两点之间的距离(一点是当前点,另一点为参数p) void input() { cout<原创 2014-05-15 21:10:39 · 473 阅读 · 0 评论 -
长方柱类
#include using namespace std;class Bulk{public: double volume(); double areas(); void input(); void output(Bulk &s);private: double length; double width; double heigth;};double Bulk::vol原创 2014-05-10 14:32:22 · 465 阅读 · 0 评论 -
实现冒泡排序
#include using namespace std;int main(){ int n,a[200];//一共n个数,n不超过200,a用来保存这些数 int i=0,j=0;//循环变量 cin>>n;//输入n个数 for(i=0;i<n;i++) cin>>a[i];//冒泡,不断比较相邻的两个数,如果顺序错了,就交换原创 2014-05-10 17:06:13 · 405 阅读 · 0 评论 -
简单的银行管理
#include #include using namespace std;class Student{private: char name[20],a,number[100]; int age,g1,g2,g3,g4; int avg;public: void input() { cin.getline(name,20,','); cin>>age; a=get原创 2014-05-10 00:13:40 · 744 阅读 · 0 评论 -
成员函数、友元函数和一般函数有区别
#include #include using namespace std;class Cpoint{private: double x;//横坐标 double y;//纵坐标public: Cpoint(double xx=0,double yy=0):x(xx),y(yy){} void distance1(Cpoint &c); friend void distanc原创 2014-05-15 23:42:33 · 504 阅读 · 0 评论 -
点类派生直线类
#include #include using namespace std;class Point //点类{public: Point():x(0),y(0){}; Point(double x0,double y0):x(x0),y(y0){}; double getX() { return x; } double ge原创 2014-05-20 16:34:55 · 580 阅读 · 0 评论 -
职员有薪水了
#include #include using namespace std;class CPerson{protected: char *m_name; char *m_id; int n_sex;//0:women,1:man int n_age;public: CPerson(char *name,char *id,int sex,int a原创 2014-05-19 23:45:08 · 412 阅读 · 0 评论 -
存储班长信息的学生类
#include #include using namespace std;class Stu //声明基类{public: Stu(int n,string nam); void display();private: int num; //学生学号 string name; //学生姓名};class StuDetail:public Stu //声明派生类S原创 2014-05-18 23:05:15 · 501 阅读 · 0 评论 -
分数类中的运算符重载
#include using namespace std;class CFraction{public: CFraction(int nu=0,int de=1); void input(); //按照"nu/de"的格式,如"5/2"的形式输入 void output(); //输出 int gys(int nu,int de);原创 2014-05-17 23:45:19 · 502 阅读 · 0 评论 -
Time类中的运算符重载
#include using namespace std;class CTime{private: unsigned short int hour; unsigned short int minute; unsigned short int second;public: CTime(int h=0,int m=0,int s=0); void setTime(); void原创 2014-05-17 19:26:30 · 417 阅读 · 0 评论 -
复数类的改进
(1)再定义一目运算符 -,-c相当于0-c。(2)定义Complex类中的>运算符的重载,实现输入和输出,改造原程序中对运算结果显示方式,使程序读起来更自然。原创 2014-05-18 11:04:33 · 496 阅读 · 0 评论 -
指向学生类的指针
#include using namespace std;class Student{public: Student(int n,double s):num(n),score(s){}; int get_num() { return num; } double get_score() { return score; } void output();private:原创 2014-05-11 18:07:19 · 557 阅读 · 0 评论