Deep Exploration C + + object Model (3)

Source: Internet
Author: User
Tags abstract implement inheritance mail object model strcmp

Introduced
Polymorphism is a powerful design mechanism that allows you to inherit an abstract public interface, encapsulate related types, and pay a price for additional indirection-whether in memory acquisition or in class decisions, C + + By using class pointer and references to support polymorphism, this program style is called "object-oriented."

Hello, Thor. About the Deep Exploration of C + + object model notes finally met with you, the speed is really OK. Well, don't waste time, just go to the subject.
This note mainly solves several questions that are often asked.
1. Does C + + support multiple inheritance?
2. What is the difference between structure and class?
3. How to design an object-oriented model?

C + + supports multiple inheritance (Java and C # do not support multiple inheritance), although I think I may not be able to use it for a lifetime (C + + is the hobby of Thor), but at least I want to know it can. The typical multiple inheritance is the following:


Iostream inherits from IStream and Ostream two classes.
Class Iostream:public Istream,public Ostream
{......};

Is there any difference between structure struct and class classes? Vchelp a few days ago also saw a post in the discussion of this issue. In fact, the structure and class really no difference, but we need to know when to use a good structure, when the class is good, of course, there is no strict rules. Usually we mix them, and from the examples in the book, we can see why we need to preserve the structure, and the book gives a method:

struct c_point{...}; This is a structure
Class Point
{
Public
Operator C_point () {return _c_point;}
//....
Private
C_point _c_point;
//....
}

This method is made into a combination (composition). It encapsulates all or part of an object model in a structure, and the benefit is that you can either apply the object model in C + + or apply it in C. Because struct encapsulates class data, both C + + and C can have a suitable space layout.

Object-oriented models are associated with a number of types that are encapsulated by an abstract base class (used to provide an interface). The real subclass is derived from it. Of course, a good design of the object model must also consider a lot of details, Thor based on his understanding to write an object-oriented model of the code, we can see, the master please to point out there is no problem. Thor thanked him first.

Train of thought: I want to implement a Personnel management management object model, Thor has been thinking about a person-managed component (of course, eventually it will be implemented in C # A business logic object, and through the database control object and database interaction, through the Web form to display the interface). Here to borrow some of their own ideas, with C + + first experiment, because just to realize the concept of object-oriented, we use object-oriented method to implement a linked list program, and there is no interface to collect information. The information is explicitly given from the Mina () function.

This object model should be able to achieve general management of people and require the following features:

Create a list of people information
Add, remove people information
Show people Information

//*************************************************
PersonnelManage.cpp
Creator: Thor
Date: 2002-8-30
Version:
Describe:
//*************************************************

The

#include
#include
//base class, which is the topmost parent class of this object model
class personnel
{
Friend class Point_list;//To implement an output list, and the ability to insert or delete people.
Protected:
Char serial_number[15];//number
Char name[10];//name
Char password[15]//password
Personnel *pointer ;
Personnel *next_link;
Public:
Personnel (char *sn,char *nm,char *pwd)
{
strcpy (SERIAL_NUMBER,SN);
strcpy (NAME,SM);
strcpy (PASSWORD,PWD);
Next_link=0;
}
Personnel ()
{
Serial_number[0]=null;
Name[0]=null;
Password[0]=null;
Next_link=0;
}
Void Fill_serial_number (char *p_n)
{
strcpy (serial_number,p_n);
}
Void Fill_name (char *p_nm)
{
strcpy (NAME,P_NM);
}
Void Fill_password (char *p_pwd)
{
strcpy (password,p_pwd);
}

Virtual void AddNew () {}
Virtual void display ()
{
cout<< "\ n Number:" < cout<< "Name:" < cout<& lt; " Password: "<}
}; The
//below is a derived subclass, in order to make it simpler for me to simplify the membership in the handle class.
//Train of thought: A member subclass is derived from the parent class, and a full member requires more detailed personal information, which omits most.
//And a full member can have some system operating privileges, which omits most.
//Full member Subclass
Class Member:public personnel
{
Friend class Point_list;
Private:
Char member_email[50];
Char member_gender[10];
Double member_age;
Public:
Member (char *sn,char *nm,char *pwd,char *em,char *gd,double AG):P Ersonnel (sn,nm,pwd)
{
strcpy ( MEMBER_EMAIL,EM);
strcpy (MEMBER_GENDER,GD);
Member_age=age;
}
Member ():P Ersonnel ()
{
Member_email[0]=null;
Member_gender=null;
member_age=0.0;
}
Void Fill_email (char *p_em)
{
strcpy (member_email,p_em);
}
Void Fill_gender (char *p_gd)
{
strcpy (MEMBER_GENDER,P_GD);
}
Void Fill_age (double ages)
{
Member_age=ages;
}

void AddNew ()
{
Pointer=this;
}
void display ()
{
Personnel::d Isplay ()
cout<< "e-mail:" < cout<< "Sex:" < cout<< "Age" <}
};

OK, we also need to implement a superclass of a super member subclass and a project manager.
This is a Super member class
Class Supermember:public member
{
Friend class Point_list;
Private
Number of documents submitted by int sm_documentcount;//
Number of code segments submitted by int sm_codecount;//
Public
Supermember (char *sn,char *nm,char *pwd,char *em,char *gd,double ag,int dc,int cc): Member (SN,NM,PWD,GD,AG)
{
sm_documnetcount=0;
sm_codecount=0;
}
Spupermember (): Member ()
{
sm_documentcount=0;
sm_codecount=0;
}
void Fill_documentcount (int smdc)
{
SM_DOCUMENTCOUNT=SMDC;
}
void Fill_codecount (int smcc)
{
SM_CODECOUNT=SMCC;
}

void AddNew ()
{
Pointer=this;
}
void display ()
{
Member::d isplay ()
cout<< "Number of articles submitted:" < cout<< "Submit code Snippets" <}
};

//Implementation friend Class
class Point_list
{
Private:
Personnel *location;
Public:
Point_list ()
{
location=0;
}
void print ();
void Insert (Personnel *node);
void Delete (char *serial_number);
}
//Display list
void Point_list::p rint ()
{
Personnel *ps=location;
while (ps!=0)
{
Ps->display ();
ps=ps->next_link;
}

//Insert list
void Point_list::insert (Personnel *node)
{
Personnel *current_node=location;
Personnel *previous_node=0;
while (current_node!=0 && strcmp (current_node->name,node->name<0)
{
Previous_node= Current_node;
current_node=current_node->next_link;
}
Node->addnew ()
node->pointer->next_link=current_node;
if (previous_node==0)
location=node->pointer;
Else
previous_node->next_link=node->pointer;
}

//delete from the list
void Point_list::d elete (char *serial_number)
{
Personnel *current_node=location;
Personnel *previous_node=0;
while (current_node!=0 && strcmp (current_node->serial_number,serial_number)!=0)
{
Previous_ Node=current_node;
current_node=current_node->next_link;
}
if (Current_node!=0 && previous_node==0)
{
location=current_node->next_link;
}
Else if (current_node!=0 && previous_node!=0)
{
previous_node->next_link=current_node- >next_link;
}
}

This is the main function, we explicitly add 3 supermember information, and then in the by number delete a
We didn't derive the admin member from the member, so there's no way to demonstrate it, but we can see it's not difficult to implement it
Note: This program is not validated and there may be bugs.
Main ()
{
Point_list PL;
Supermember sm1 ("000000000000001", "Thunder God", "123456", "lsmodel@ai361.com", "male", 29.9,10,10);
Supermember sm1 ("000000000000002", "Wood One", "234567", "MY@ai361.com", "male", 26.5,20,5);
Supermember sm1 ("000000000000003", "Deciduous Summer", "345678", "LYXR@ai361.com", "male", 24.8,5,15);
If we also derive managers, the following might be the way:
Managemember mm1 ("000000000000004", "ADMIN", "888888", "webmaster@ai361.com", "male", 30,5,15,......);

The following is a list of 3 people listed above
Pl.insert (&SM1);
Pl.insert (&SM2);
Pl.insert (&SM3);
Pl.insert (&MM1) of the corresponding management staff;

Below is a display of their
Here is the list of people to display
Pl.print ();

The following is the deletion of a person's information
Pl.delete ("000000000000001");
Let's show one more time.
cout<< "\ n deleted list: \ n";
Pl.print ();
}

The program is not on the computer verification, in my mind to run a bit, I think the output should be like this:

Item No: 000000000001
Name: Thunder God
Password: 123456
E-mail: lsmodel@ai361.com
Sex: Male
Age: 29.9
Number of articles submitted: 10
Number of submissions: 10

Item No: 000000000002
Name: Wooden One
Password: 234567
E-mail: MY@21CN.com
Sex: Male
Age: 26.5
Number of articles submitted: 20
Number of submissions: 5

Item No: 000000000003
Name: Summer of deciduous
Password: 345678
E-mail: LYXR@163.com
Sex: Male
Age: 24.8
Number of articles submitted: 5
Number of submissions: 15

The list after deletion:

Item No: 000000000002
Name: Wooden One
Password: 234567
E-mail: MY@21CN.com
Sex: Male
Age: 26.5
Number of articles submitted: 20
Number of submissions: 5

Item No: 000000000003
Name: Summer of deciduous
Password: 345678
E-mail: LYXR@163.com
Sex: Male
Age: 24.8
Number of articles submitted: 5
Number of submissions: 15

From the above example, I think we can understand the benefits of the object model, we use a lot of pointers and references to complete polymorphic properties. Unlike the database examples in the book, we have one more layer, because I consider that people may be anonymous or registered, so in order to differentiate them, Two layers are used to complete the interface, and then all registered members are people who derive different permissions from the member class, such as super members and administrative personnel.

Finally, summarize it with a passage from the book. P34

In short, polymorphism is a powerful design mechanism that allows you to inherit an abstract public interface, encapsulate the associated type, and pay a price for the extra indirection-whether it's in memory, or in class decisions, C + + By using class pointer and references to support polymorphism, this program style is called "object-oriented."

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.