C++クラスの設計と継承
3787 ワード
1、コンパイラはクラスのデフォルトのコンストラクタ、レプリケーションコンストラクタ、割り当て関数、解析関数を生成します.もちろん、デフォルトのこれらの関数は、オブジェクトのメンバーを簡単に割り当てるだけです.
2、クラスのメンバーオブジェクトにnewでメモリを動的に割り当てる必要があるメンバーが含まれている場合は、レプリケーションコンストラクタ、リロード値オペレータ、解析関数、パラメータ付きコンストラクタ、デフォルトのコンストラクタを再定義する必要があります.
3、派生クラス構築の場合、まずベースクラスのコンストラクタ(派生クラスのコンストラクタタイプに応じて、ベースクラスの対応する形式のコンストラクタを呼び出す)を呼び出し、次に派生タイプのコンストラクタを呼び出す.
4.クラスのライフサイクルが終了すると、派生クラスの構造関数が呼び出され、次にベースクラスの構造関数が呼び出されます.
5、コンストラクション関数とコンストラクション関数を継承することはできません.それぞれのメンバーの付与とコンストラクションを処理します.
6、オブジェクトの生命の開始、終了、およびメモリ内のオブジェクトの位置(スタック上、スタック上?)、オブジェクトメンバーのメモリレイアウトは、上記の4つのポイントを理解し、メモリの漏洩を防ぐ鍵です.
以下のコードは以上の6点について実験を行い、よく考えます.ベースクラスファイルpeople.h、people.cpp,派生クラスファイルwoman.h、woman.cpp,主関数ファイルtestmain.cpp
2、クラスのメンバーオブジェクトにnewでメモリを動的に割り当てる必要があるメンバーが含まれている場合は、レプリケーションコンストラクタ、リロード値オペレータ、解析関数、パラメータ付きコンストラクタ、デフォルトのコンストラクタを再定義する必要があります.
3、派生クラス構築の場合、まずベースクラスのコンストラクタ(派生クラスのコンストラクタタイプに応じて、ベースクラスの対応する形式のコンストラクタを呼び出す)を呼び出し、次に派生タイプのコンストラクタを呼び出す.
4.クラスのライフサイクルが終了すると、派生クラスの構造関数が呼び出され、次にベースクラスの構造関数が呼び出されます.
5、コンストラクション関数とコンストラクション関数を継承することはできません.それぞれのメンバーの付与とコンストラクションを処理します.
6、オブジェクトの生命の開始、終了、およびメモリ内のオブジェクトの位置(スタック上、スタック上?)、オブジェクトメンバーのメモリレイアウトは、上記の4つのポイントを理解し、メモリの漏洩を防ぐ鍵です.
以下のコードは以上の6点について実験を行い、よく考えます.ベースクラスファイルpeople.h、people.cpp,派生クラスファイルwoman.h、woman.cpp,主関数ファイルtestmain.cpp
#ifndef PEOPLE_H
#define PEOPLE_H
class people
{
private:
char *name;
public:
people(); //
people(const char *s); //
people(const people& p);//
people & operator=(const people& p); //
~people();
};
#endif
#include "people.h"
#include <stdio.h>
#include <string.h>
people::people() //
{
printf(" people()
");
name = new char[strlen("hello")+1]; //
strcpy(name,"hello");
}
people::people(const char *s) //
{
printf(" people(const char* s)
");
name = new char[strlen(s)+1];
strcpy(name,s);
}
people::people(const people & p)
{
printf(" people(const people &p
");
name = new char[strlen(p.name)+1];
strcpy(name,p.name);
}
people & people::operator=(const people &p)
{
printf(" operator=
");
if(this == &p)
return *this;
else
{
delete name;
name = new char[strlen(p.name)+1];
strcpy(name,p.name);
return *this;
}
}
people::~people()
{
printf(" ~people()
");
delete name;
}
#ifndef WOMAN_H
#define WOMAN_H
#include "people.h"
class woman : public people
{
private:
char *company; // ,new ,
int age;
public:
woman();//
woman(const char * c,int a,const char * s); //
woman(const woman & w); //
woman & operator=(const woman &w); //
~woman();
};
#endif
#include "woman.h"
#include <stdio.h>
#include <string.h>
woman::woman()
{
printf(" woman()
");
char *s = "world";
company = new char[strlen(s)+1];
strcpy(company,s);
age = 99;
}
woman::woman( const char * c,int a,const char * s):people(s)
{
printf(" woman( const char * s,int a)
");
company = new char[strlen(c)+1];
strcpy(company,c);
age = a;
}
woman::woman(const woman &w):people(w) // , , people(const people & p)
{ // people, woman,
printf(" woman(const woman &w)
");
company = new char[strlen(w.company)+1];
strcpy(company,w.company);
age = w.age;
}
woman & woman::operator=(const woman & w) // ,
{
printf(" woman::operator=(const woman & w)
");
if (this == &w)
return *this; //
else
{
delete company; //
people::operator=(w); // , , ,
company = new char[strlen(w.company)+1];
strcpy(company,w.company);
return *this;
}
}
woman::~woman()
{
printf(" ~woman()
");
delete company;
}