1580 views|0 replies

6570

Posts

0

Resources
The OP
 

Let's talk about the analysis of C++ constructors and destructors [Copy link]

When creating an object, it is often necessary to do some initialization work, such as assigning initial values to data members. Note that data members of a class cannot be initialized when declaring the class. If all members in a class are public, the data members can be initialized when defining the object. For example: class Time { public : //Declare as public members hour; minute; sec; }; Time t1={14,56,30}; //Initialize t1 to 14:56:30 This situation is similar to the initialization of structure variables. The values of each public data member are listed in sequence in a curly brace, and the two values are separated by commas. However, if the data member is private, or there are private or protected members in the class, this method cannot be used for initialization. Here are a few examples (C++ object-oriented programming examples) that use member functions to assign initial values to data members in an object (such as the set_time function in Example 8.3). As can be seen from Example 8.3, the user calls the set_time function in the main function to assign values to data members. If multiple objects are defined for a class, and there are many data members in the class, then the program will appear very bloated and cumbersome. The role of the constructor To solve this problem, C++ provides a constructor to handle the initialization of objects. The constructor is a special member function. Unlike other member functions, it does not require the user to call it, but is automatically executed when the object is created. The name of the constructor must be the same as the class name and cannot be arbitrarily named by the user, so that the compilation system can recognize it and treat it as a constructor. It does not have any type and does not return any value. The function of the constructor is defined by the user, and the user designs the function body and function parameters according to the initialization requirements. [Example 9.1] Define the construction member function based on Example 8.3. #include
using namespace std;
class Time
{
public :
Time( )
{
hour=0;
minute=0;
sec=0;
}
void set_time( );
void show_time( );
private :
int hour;
int minute;
int sec;
};
void Time::set_time( )
{
cin>>hour;
cin>>minute;
cin>>sec;
}
void Time::show_time( )
{
cout<
#include
using namespace std; class Student //Declare the Student class { public : Student(int n,string nam,char s ) //Define the constructor { num=n; name=nam; sex=s; cout<<"Constructor called."<
Let's talk about the analysis of C++ constructors and destructors


This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list