C + + class and initialization

Source: Internet
Author: User

--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

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.