C++ Primer Plus学习:第十二章 类和动态内存(1)

本文详细介绍了C++中的静态数据成员的概念及其初始化方式,同时讲解了如何使用new和delete进行内存分配与释放,通过具体实例展示了这些概念的应用。
静态数据成员
  静态数据成员在类声明中声明,在包含类方法的文件中初始化
  初始化时使用作用域操作符来指出静态成员所属的类,但如果静态成员是整形或者枚举型const,则可以在类声明中初始化
new、delete
  在构造函数中使用new来分配内存,必须在相应析构函数中使用delete来释放内存。
  若使用new[](包括中括号)来分配内存,则应使用delete[](包括中括号)来释放内存。

strngbad.h

#include <iostream>
#ifndef STRNGBAD_H_
#define STRNGBAD_H_
class StringBad
{
      private:
              char * str;
              int len;
              static int num_strings;
      public:
             StringBad(const char * s);
             StringBad();
             ~StringBad();
             //friend function
             friend std::ostream & operator<<(std::ostream & os,const StringBad & st);
      };
#endif


strngbad.cpp

#include <cstring>
#include "strngbad.h"
using std::cout;

int StringBad::num_strings = 0;

StringBad::StringBad(const char * s)
{
                           len = std::strlen(s);
                           str = new char[len+1];
                           std::strcpy(str,s);
                           num_strings++;
                           cout<<num_strings<<": \""<<str<<"\" object created\n";
                           }
                           
                           
StringBad::StringBad()
{
                      len = 4;
                      str = new char[4];
                      std::strcpy(str,"C++");
                      num_strings++;
                      cout<<num_strings<<": \""<<str<<"\" default object created \n";
                      }
                      
StringBad::~StringBad()
{
                       cout<<"\""<<str<<"\" object deleted, ";
                       --num_strings;
                       cout<<num_strings<<" left\n";
                       delete []str;
                       }
                       
std::ostream & operator<<(std::ostream & os,const StringBad & st)
{
             os<<st.str;
             return os;
             }


vennews.cpp

#include <iostream>
#include "strngbad.h"
using std::cout;
void callme1(StringBad &);
void callme2(StringBad);

int main()
{
    using std::endl;
    StringBad headline1("Celery Stalks at Midnight");
    StringBad headline2("Lettuce Prey");
    StringBad sports("Spinach Leaves Bowl for Dollars");
    cout<<"headline1: "<<headline1<<endl;
    cout<<"headline2: "<<headline2<<endl;
    cout<<"sports: "<<sports<<endl;
    callme1(headline1);
    cout<<"headline1: "<<headline1<<endl;
    callme2(headline2);
    cout<<"headline2: "<<headline2<<endl;
    cout<<"Initialize one object to another: \n";
    StringBad sailor = sports;
    cout<<"sailor: "<<sailor<<endl;
    cout<<"Assign one object to another: \n";
    StringBad knot;
    knot = headline1;
    cout<<"knot: "<<knot<<endl;
    cout<<"End of main()\n";
    
    system("pause");
    return 0;
    }
    
void callme1(StringBad & rsb)
{
     cout<<"String passed by reference:\n";
     cout<<"       \""<<rsb<<"\"\n";
     }
     
void callme2(StringBad sb)
{
     cout<<"String passed by value:\n";
     cout<<"       \""<<sb<<"\"\n";
     }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值