Using the default methods supplied by the class C++ program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the default methods supplied by the class instead of the overriding it in C++ programming

To show: How to use the default methods of the class instead of overriding it in C++ programming

 

 

 

// using the default methods C++ example

#include <iostream>

#include <string>

using namespace std;

 

// class declaration

class def

{

// private by default

int size; //a simple stored value

char *string; //a name for the stored data

public:

// this overrides the default constructor

def(void);

// this overrides the default copy constructor

def(def &in_object);

// this overrides the default assignment operator

def &operator=(def &in_object);

// this destructor should be required with dynamic allocation

~def(void);

// and finally, a couple of ordinary methods

void set_data(int in_size, char *in_string);

void get_data(char *out_string);

};

 

// class implementation

def::def(void)

{

size = 0;

string = new char[2];

// strcpy(string, "");

strcpy_s(string, 2, "");

}

 

def::def(def &in_object)

{

size = in_object.size;

string = new char[strlen(in_object.string) + 1];

// strcpy(string, in_object.string);

strcpy_s(string, 50, in_object.string);

}

 

def& def::operator=(def &in_object)

{

delete [ ] string;

size = in_object.size;

string = new char[strlen(in_object.string) + 1];

// strcpy(string, in_object.string);

strcpy_s(string, 40, in_object.string);

return *this; //this pointer

}

def::~def(void)

{

delete [ ] string;

}

void def::set_data(int in_size, char *in_string)

{

size = in_size;

delete [ ] string;

string = new char[strlen(in_string) + 1];

// strcpy(string, in_string);

strcpy_s(string, 50, in_string);

}

void def::get_data(char *out_string)

{

char temp[10];

// strcpy(out_string, string);

strcpy_s(out_string, 50, string);

// strcat(out_string, " = ");

strcat_s(out_string, 50, " = ");

// itoa(size, temp, 10);, _itoa() is ISO standard & _itoa_s() is a secure version

_itoa_s(size, temp, sizeof(temp), 10);

// strcat(out_string, temp);

strcat_s(out_string, 50, temp);

}

 

// main program

void main()

{

char buffer[80];

def my_data;

 

my_data.set_data(8, " small size, override default constructor ");

my_data.get_data(buffer);

 

cout<<" content of buffer!!\n"<<buffer<<"\n";

def more_data(my_data);

more_data.set_data(12, " medium size, override copy constructor");

my_data.get_data(buffer);

cout<<"\n content of buffer 2nd round!!\n"<<buffer<<"\n";

my_data = more_data;

my_data.get_data(buffer);

cout<<"\n content of buffer 3rd round, assignment overload!!\n"<<buffer<<"\n";

}

 

Output example:

 

content of buffer!!

small size, override default constructor = 8

content of buffer 2nd round!!

small size, override default constructor = 8

content of buffer 3rd round, assignment overload!!

medium size, override copy constructor = 12

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index