C++/Class/Destructor
Содержание
- 1 Constructing and Destructing sequence for two base classes
- 2 Define and use the destructor
- 3 Define destrcuctor outside the class definition
- 4 Derived class call its base constructor
- 5 Derived constructor uses no parameters
- 6 Implement a destructor
- 7 System will call the destructor
- 8 Using a constructor and destructor.
Constructing and Destructing sequence for two base classes
#include <iostream>
using namespace std;
class BaseClass1 {
public:
BaseClass1() { cout << "Constructing BaseClass1\n"; }
~BaseClass1() { cout << "Destructing BaseClass1\n"; }
};
class BaseClass2 {
public:
BaseClass2() { cout << "Constructing BaseClass2\n"; }
~BaseClass2() { cout << "Destructing BaseClass2\n"; }
};
class DerivedClass: public BaseClass1, public BaseClass2 {
public:
DerivedClass() { cout << "Constructing DerivedClass\n"; }
~DerivedClass() { cout << "Destructing DerivedClass\n"; }
};
int main()
{
DerivedClass ob;
return 0;
}
Define and use the destructor
#include <iostream>
using namespace std;
class myclass {
int a;
public:
myclass(); // constructor
~myclass(); // destructor
void show();
};
myclass::myclass()
{
cout << "In constructor\n";
a = 10;
}
myclass::~myclass()
{
cout << "Destructing...\n";
}
void myclass::show()
{
cout << a << endl;
}
int main()
{
myclass ob;
ob.show();
return 0;
}
Define destrcuctor outside the class definition
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
class Book
{
public:
char title[256];
char author[64];
float price;
Book(char *title, char *author, char *publisher, float price);
~Book(void);
void show_title(void) { cout << title << "\n"; };
float get_price(void) { return(price); };
void show_book(void)
{
show_title();
show_publisher();
};
void assign_publisher(char *name) { strcpy(publisher, name); };
private:
char publisher[256];
void show_publisher(void) { cout << publisher << "\n"; };
};
Book::Book(char *title, char *author, char *publisher, float price)
{
strcpy(Book::title, title);
strcpy(Book::author, author);
strcpy(Book::publisher, publisher);
Book::price = price;
}
Book::~Book(void)
{
cout << "Destructing the instance " << title << "\n";
}
int main(void)
{
Book tips("A", "B", "C", 49.95);
Book diary("D", "E", "F", 9.95);
tips.show_book();
diary.show_book();
}
Derived class call its base constructor
#include <iostream>
using namespace std;
class BaseClass {
protected:
int i;
public:
BaseClass(int x) {
i = x;
cout << "Constructing base\n";
}
~BaseClass() {
cout << "Destructing base\n";
}
};
class DerivedClass: public BaseClass {
int j;
public:
DerivedClass(int x, int y): BaseClass(y) {
j = x;
cout << "Constructing DerivedClass\n";
}
~DerivedClass() {
cout << "Destructing DerivedClass\n";
}
void show() {
cout << i << " " << j << endl;
}
};
int main()
{
DerivedClass ob(3, 4);
ob.show();
return 0;
}
Derived constructor uses no parameters
#include <iostream>
using namespace std;
class BaseClass1 {
protected:
int i;
public:
BaseClass1(int x) {
i =x;
cout << "Constructing BaseClass1\n";
}
~BaseClass1() {
cout << "Destructing BaseClass1\n";
}
};
class BaseClass2 {
protected:
int k;
public:
BaseClass2(int x) {
k = x;
cout << "Constructing base2\n";
}
~BaseClass2() {
cout << "Destructing base2\n";
}
};
class DerivedClass: public BaseClass1, public BaseClass2 {
public:
DerivedClass(int x, int y): BaseClass1(x), BaseClass2(y) {
cout << "Constructing DerivedClass\n";
}
~DerivedClass() {
cout << "Destructing DerivedClass\n";
}
void show() {
cout << i << " " << k << endl;
}
};
int main()
{
DerivedClass ob(3, 4);
ob.show();
return 0;
}
Implement a destructor
#include <iostream>
using namespace std;
class prompt {
int count;
public:
prompt(char *s) {
cout << s; cin >> count;
};
~prompt();
};
prompt::~prompt() {
int i, j;
for(i = 0; i <count; i++) {
cout << "\a";
for(j=0; j<32000; j++)
; // delay
}
}
int main()
{
prompt ob("Enter a number: ");
return 0;
}
System will call the destructor
#include <iostream>
using namespace std;
class myclass {
public:
int who;
myclass(int id);
~myclass();
};
myclass::myclass(int id)
{
cout << "Initializing " << id << endl;
who = id;
}
myclass::~myclass()
{
cout << "Destructing " << who << endl;
}
int main()
{
myclass object1(3);
cout << "Here.\n";
myclass object2(4);
return 0;
}
Using a constructor and destructor.
#include <iostream>
using namespace std;
#define SIZE 10
class stack {
int stck[SIZE];
int topOfStack;
public:
stack(); // constructor
~stack(); // destructor
void push(int i);
int pop();
};
// constructor
stack::stack(){
topOfStack = 0;
cout << "Stack Initialized\n";
}
// destructor
stack::~stack(){
cout << "Stack Destroyed\n";
}
void stack::push(int i){
if( topOfStack == SIZE ) {
cout << "Stack is full.\n";
return;
}
stck[ topOfStack ] = i;
topOfStack++;
}
int stack::pop() {
if( topOfStack == 0 ) {
cout << "Stack underflow.\n";
return 0;
}
topOfStack--;
return stck[ topOfStack ];
}
int main()
{
stack a, b;
a.push(1);
b.push(2);
a.push(3);
b.push(4);
cout << a.pop() << " ";
cout << a.pop() << " ";
cout << b.pop() << " ";
cout << b.pop() << endl;
return 0;
}