码迷,mamicode.com
首页 > 编程语言 > 详细

C++第七天笔记2016年02月24日(周三)P.M

时间:2016-02-25 22:44:03      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:

1.    哪些构造函数支持做类型转换?
       传递一个参数就可以被调用的构造函数。

2.    不同函数调用时间分析源码:

 

 1 #include <iostream>
 2 using namespace std;
 3 
 4 class Demo{
 5 public:
 6     Demo(int n=0):i(n){cout<<"默认构造函数被调用"<<endl;}
 7     Demo(const Demo& a)
 8     {
 9         i=a.i;
10         cout<<"拷贝构造函数被调用"<<endl;
11     }
12     ~Demo(){cout<<"构造函数被析构"<<endl;}
13     const Demo& operator=(const Demo& a)
14     {
15         this->i=a.i;
16         cout<<"赋值运算符重载被调用"<<endl;
17         return *this;
18     }
19     Demo& set_value(int n){i=n;return *this;}
20     int get_value(){return i;}
21 private:
22     int i;
23 };
24 Demo foo(Demo x){//函数被调用时,初始化新对象x会调用拷贝构造函数
25     Demo d;//声明对象d调用默认构造函数
26     return d;//以值的方式返回对象时,编译器会为返回值生成一个中间对象(即使用d拷贝生成新对象)会调用拷贝构造函数。
27 }//函数调用结束时,对象x和d的作用域结束,分别调用析构函数回收d和x的资源。
28 int main(int argc, const char * argv[]) {
29     
30     Demo a(2);//声明对象a会调用默认构造函数
31     {
32         Demo b;//声明对象b时默认构造函数被调用
33         b=foo(a);
34         /*调用函数foo(跳转到foo函数处查看函数具体调用情况说明)。一旦函数调用结束,赋值重载函数会被调用(将右值赋值给左值)  赋值结束后,foo(a)表达式的值(即返回的中间对象)使用结束,会调用析构函数回收其资源。*/
35     }//块域结束,则对象b作用域结束,析构函数会被调用。
36     Demo c=a;//用一个已经存在的对象a创建并初始化新对象c 会调用拷贝构造函数
37     return 0;
38 }//main函数域结束,则对象a c作用域结束,会调用析构函数分别回收c a的资源

 

3.  构造函数类型转换:

 1 #include <iostream>
 2 #include "cstring"
 3 using namespace std;
 4 
 5 class String
 6 {
 7 public:
 8     String():len(0),rep(new char[1]){
 9         strcpy(rep, "");
10     }
11     String(char* s)
12     {
13         if (!s) {
14             len=0;
15             rep=new char[1];
16             strcpy(rep, "");
17         }
18         else{
19             len=(int)strlen(s);
20             rep=new char[len+1];
21             strcpy(rep,s);
22         }
23     }
24     String(const String& str):len(str.len),rep(new char[str.len+1]){
25         strcpy(rep, str.rep);
26     }
27     ~String(){
28         delete [] rep;
29     }
30     friend void print(const String& s);
31 private:
32     char* rep;
33     int len;
34 };
35 void print(const String& s)
36 {
37     cout<<s.rep<<endl;
38 }
39 int main(int argc, const char * argv[]) {
40     char* a="aaaaaaaa";
41     print(a);
42     print("11111");
43     String b="bbbbbbbb";
44     print(b);
45     return 0;
46 }

4.  构造函数与对象数组源码:

 1 #include <iostream>
 2 #include "cstring"
 3 using namespace std;
 4 class Item
 5 {
 6 public:
 7     Item():name(new char[1]),id(0){strcpy(name, "");}
 8     Item(char* s,int i=0):id(i),name(new char[strlen(s)+1]){
 9         strcpy(name, s);
10     }
11     void set(char* s,int i){
12         if (name) {
13             delete [] name;
14             name=new char[strlen(s)+1];
15             strcpy(name, s);
16             id=i;
17         }
18     }
19     ~Item(){delete [] name;}
20     friend ostream& operator<<(ostream& out,const Item& i);
21 private:
22     char* name;
23     int id;
24 };
25 
26 ostream& operator<<(ostream& out,const Item& i)
27 {
28     out<<endl<<i.name<<" "<<i.id;
29     return out;
30 }
31 int main(int argc, const char * argv[]) {
32     Item stat[2]={Item("pen",2334),Item("note",3155)};
33     Item meat[3]={"fd","dfs","rtfe"};
34     Item nothing[2];
35     Item* ptr=new Item[3];
36     for (int i=0; i<2; ++i) {
37         cout<<stat[i];
38     }
39     for (int i=0; i<3; ++i) {
40         cout<<meat[i];
41     }
42     for (int i=0; i<2; ++i) {
43         nothing[i].set("djkjdf", i+1);
44         cout<<nothing[i];
45     }
46     for (int i=0; i<3; ++i) {
47         ptr[i].set("dags", i+1);
48         cout<<ptr[i];
49     }
50     delete [] ptr;
51     return 0;
52 }

5. 补充:内存分析(第一次大作业)

#include <iostream>
#include"cstring"

using namespace std;

class Person
{
public:
    Person(char* _name="jack",int _age=0):name(new char[strlen(_name)+1]),age(_age){
        strcpy(name, _name);
    }
    ~Person(){
        cout<<"析构"<<endl;
        delete [] name;
    }
private:
    char* name;
    int age;
};

int main(int argc, const char * argv[]) {
    Person** elements=new Person*[5];//对象数组
    Person* p1=new Person;
    Person* p2=new Person;
    Person* p3=new Person;
    elements[0]=p1;
    elements[1]=p2;
    elements[2]=p3;
    delete [] elements;//只会回收elsments所指向的堆空间,不会调用析构函数。
    delete p1;//p1所指向的堆空间中存储的是对象,空间回收时对象被销毁,析构函数会被调用。
    delete p2;
    delete p3;
    return 0;
}

技术分享

 

C++第七天笔记2016年02月24日(周三)P.M

标签:

原文地址:http://www.cnblogs.com/cai1432452416/p/5218531.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!