--c++ class and initialization
--------------------------------2014/09/04
1. The definition of a class (header file, declaration file) is considered an external interface of the class and is generally written as an. h header file.
2. The member function definition (source file) of a class is considered an internal implementation of the class and is generally written as a. cpp/.cc file.
member function definitions
return value class Name:: Function name (argument list) {
function body;}
Class definition
Class Name {
Member variables
member functions
}; --Notice there's a semicolon here .
See a simple example:
Student.h
#include <string>using namespacestd;classStudent { Public: voidSet_name (stringv_name); voidSet_age (intv_age); voidSet_school_name (stringv_school_name); stringget_name (); intGet_age (); stringget_school_name ();Private: stringname; intAge ; stringschool_name;};
student.cc
#include"student.h"voidStudent::set_name (stringv_name) {Name=V_name;} voidStudent::set_age (intv_age) { Age=V_age;} voidStudent::set_school_name (stringv_school_name) {School_name=V_school_name;} stringStudent::get_name () {returnname;} intStudent::get_age () {returnAge ;} stringStudent::get_school_name () {returnSchool_name;}
main.cc
#include <iostream>#include"student.h"using namespacestd;intMainintargcChar*argv[]) {Student*a=Newstudent (); A->set_name ("Jack"); A->set_age ( -); A->set_school_name ("Haford"); cout<<a->get_name () <<" "<<a->get_age () <<" "<<a->get_school_name () <<Endl;}
compiling source files
main.cc student.cc-- source file compilation [[email protected] student]#. Haford
C + + class and initialization