C++语言基础 例程 析构函数

贺老师的教学链接  本课讲解

析构函数示例

#include<string>
#include<iostream>
using namespace std;
class Student
{
public:
    Student(int n,string nam,char s)
    {
        num=n;
        name=nam;
        sex=s;
        cout<<"执行构造函数:"<<name<<" come."<<endl;
    }
    ~Student( )
    {
        cout<<"执行析构函数:"<<"Bye bye, "<<name<<"."<<endl;
    }
    void display( )
    {
        cout<<"num: "<<num<<endl;
        cout<<"name: "<<name<<endl;
        cout<<"sex: "<<sex<<endl<<endl;
    }
private:
    int num;
    string name;
    char sex;
};
int main( )
{
    Student stud1(10010,"Wang_li",'f');
    stud1.display( );
    Student stud2(10011,"Zhang_fun",'m');
    stud2.display( );
    return 0;
}
时间: 2024-07-30 08:49:38

C++语言基础 例程 析构函数的相关文章

C++语言基础 例程 虚析构函数

贺老师的教学链接  本课讲解 问题的由来 #include <iostream> using namespace std; class Point { public: Point( ) { } ~Point() { cout<<"executing Point destructor"<<endl; } }; class Circle:public Point { public: Circle( ) { } ~Circle( ) { cout<&

C++语言基础 例程 派生类的构造函数和析构函数

贺老师的教学链接  本课讲解 一个简单派生类的定义 #include <iostream> #include<cstring> using namespace std; class Student //声明基类Student { public: Student(int n,string nam,char s):num(n),name(nam),sex(s) {} //基类构造函数 ~Student( ) { } //基类析构函数 void show( ) { cout<<

C++语言基础 例程 调用构造函数和析构函数的顺序

贺老师的教学链接  本课讲解 析构函数应用实例方案1 const int N =500; class Salary { public: Salary(int); void input( ); void show( ); ~Salary(); private: double salarys[N]; int number; //实际人数 }; 方案2 class Salary { public: Salary(int); void input( ); void show( ); ~Salary();

C++语言基础 例程 应用系统开发:银行储蓄系统

贺老师的教学链接  本课讲解 说明:(1)下面的代码,只演示了利用链表作为存储结构的可选处理方法,本讲提到的其他方面的拓展,请感兴趣做下去的同学自行使用相关技术组合起来,形成一个完整的系统.(2)运行程序,登录用户名和密码,请阅读程序,从程序中找出.建议建立多文件项目,将代码拷贝到IDE中看.(3)本程序由我的2011级学生刘镇参加企业组织的实训中完成,原文在:点击打开链接 Record.h #ifndef HEADER_RECORD //条件编译 #define HEADER_RECORD #

C++语言基础 例程 异常处理的方法

贺老师的教学链接  本课讲解 例:防止除数为0 #include <iostream> using namespace std; template <typename T> T Div(T x,T y) { if(y==0) throw y;//抛出异常 return x/y; } int main() { int x=5,y=0; double x1=5.5,y1=0.0; try { //被检查的语句 cout<<x<<"/"<

C++语言基础 例程 案例:MyVector类的设计

贺老师的教学链接  本课讲解 //MyVector类的设计 #include <iostream> using namespace std; class MyVector //定义向量类 { public: MyVector(int m); //构造函数,共有m个元素的向量,元素值预置为0 MyVector(const MyVector &v); //复制构造函数 ~MyVector(); //析构函数:释放动态数组所占用的存储空间 friend istream &operat

C++语言基础 例程 对象的动态建立和释放

贺老师的教学链接  本课讲解 对象的动态建立和释放 #include<iostream> using namespace std; class Box { public: Box(int w,int l,int h); ~Box(); int width; int length; int height; }; Box::Box(int w,int l,int h) { width=w; length=l; height=h; cout<<"========调用构造函数==

C++语言基础 例程 C++中的输入和输出

贺老师的教学链接 程序将自行识别符号 #include <iostream> using namespace std; int main() { int a,b; char op; cin>>a>>op>>b cout<<"result: "<<'\n'; cout<<"a: "<<a<<'\n'; cout<<"b: "<

C++语言基础 例程 重载流插入运算符和流提取运算符

贺老师的教学链接  本课讲解 重载流插入运算符"<<" #include <iostream> using namespace std; class Complex { public: Complex( ) { real=0; imag=0; } Complex(double r,double i) { real=r; imag=i; } Complex operator + (Complex &c2); //运算符"+"重载为成员函