第九周-程序阅读

/*Copyright (c)2016,烟台大学计算机与控制工程学院
 *All rights reserved.
 *文件名称:my.cpp
 *作    者:张瀚文
 *完成日期:2016年5月6日
 *
*问题描述:
阅读程序,写出程序的运行结果并理解其运行机制。
*/
#include <iostream>
#include<cstring>
using namespace std;
class A
{
  char *a;
  public:
      A(A &t);
      A(char *aa)
     {
          a=new char[strlen(aa)+1];//这样做的意义在于:可以动态分配内存空间,根据实际需要,更加有效利用内存;
          strcpy(a,aa);//数据成员a与形式参数aa的关系:aa为形参,根据aa的长度返回值,+1为结束字符,然后 aa的字符串拷贝到a中。
      }
      ~A(){
      delete[]a;//当主函数结束时,进行析构函数,将之前动态分配的内存释放。

      }
     void output(){cout<<a<<endl;}
};
A::A(A &t)
{
a=new char[strlen(t.a)+1];//这样做的意义在于:可以动态分配内存空间,根据实际需要,更加有效利用内存;
          strcpy(a,t.a);

}
int main()
{
    A a("good monring ,code monkeys!");
    a.output();
    A b(a);

    b.output();
    return 0;
}

时间: 2024-08-15 04:22:29

第九周-程序阅读的相关文章

第九周 程序阅读-学生信息管理系统

阅读程序"简单C++学生信息管理系统",找出其中出现构造函数.友元函数.运算符重载.静态数成员语法现象出现的位置,仔细体会其用法,在以后的设计中能够灵活应用有关方法和技巧. #include <iostream> #include <cstring> using namespace std; #define MAX 100 class CDate // 定义日期类 { private: unsigned short int year; // 年 unsigned

第九周 程序阅读-字符串类的设计

阅读下面的程序,领会其中用到的设计方案.技术手段与算法. /* 对于要定义的字符串类CMyString, 数据成员包括: - 字符串的长度: - 指向字符串第一个字符的指针 成员函数包括: - 不带参数的构造函数: - 带一个类型为const char *类型的参数(用于对字符串初始化)的构造函数: - 带一个const CMyString&类型的复制构造参数: - 析构函数: - Strlen函数 (用于求字符串的长度): - int Find(char c) (找出字符c在本字符串中第一次出

第九周-程序阅读理解

/*Copyright (c)2016,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:my.cpp *作 者:张瀚文 *完成日期:2016年5月6日 * *问题描述: 阅读程序,写出程序的运行结果并理解其运行机制. */ #include <iostream> #include<cstring> using namespace std; class A { char *a; public: A(char *aa) { a=new char[s

第15周 程序阅读-二进制文件及文件的读取1

1.阅读并运行下面的两个程序,分别用记事本和二进制文件阅读器(请自行下载Binary Viewer等程序,或者用DOS中的Debug程序,并百度其用法).查看其内容,并理解文件存储的原理. (1) #include <iostream> #include <fstream> #include <cstdlib> using namespace std; int main( ) { int a; ofstream outfile("f1.dat",io

第15周 程序阅读-二进制及二进制文件的读取3

3.阅读下面的程序,指出其功能,体会seekg().tellg()等函数的功能及其用法 (1) #include<iostream> #include <fstream> using namespace std; const char * filename = "a.txt"; int main () { long l,m; ifstream file (filename, ios::in|ios::binary); l = file.tellg(); file

第二周 程序阅读

#include <iostream> #include <cstring> using namespace std; class Student { private: int num; char name[20]; char sex; public: void set_data(int n, char *p,char s) { num=n; strcpy(name,p); sex=s; } void display( ) { cout<<"num: &quo

第十二周程序阅读5:基类和派生类的转换

问题及代码: #include <iostream> using namespace std; class A { protected: int a,b; public: A(int aa, int bb):a(aa), b(bb) {} void printA() { cout<<"a: "<<a<<"\tb: "<<b<<endl; } }; class B: public A { int

第15周 程序阅读-二进制及二进制文件的读取2

2.查看下面程序的输出,解释为什么会有这样的输出. #include <iostream> #include <fstream> using namespace std; int main( ) { unsigned char a[] = {0x32,0x30,0x31,0x35,0xA3,0xAC,0xCE,0xD2,0xC3,0xC7,0xB3,0xD4, 0xC1,0xCB,0xB5,0xDA,0xD2,0xBB,0xBF,0xDA,0xF3,0xA6,0xD0,0xB7};

第15周 程序阅读-二进制文件及文件的读取4

4.阅读并运行下面的示例,体会二进制文件和字符串流操作的一般方法. 例17 #include <strstream> #include<iostream> using namespace std; struct student {     int num;     char name[20];     float score; }; int main( ) {     student stud[3]={1001,"Li",78,1002,"Wang&q