Simple organization of C + + multi-source files

Source: Internet
Author: User

Simple organization of C + + multi-source files

Basically the same as C, except that the C + + method is declared in the class. See a simple example.
Information publicly available in the Ainimal.h class.


Click ( here) to collapse or open

  1. #ifndef _animal_h__
  2. #define _animal_h__
  3. #include <iostream>
  4. using namespace Std;
  5. Class Animal{
  6. Private:
  7. string name;
  8. Public:
  9. void print(void);
  10. Animal(string name){This->name=name;}
  11. };
  12. #endif

The specifics of the implementation of the methods in the Animal.cpp class, or the hidden parts, I have added a private function extra_info,static decoration for this file.


Click ( here) to collapse or open

  1. #include "Animal.h"
  2. Static string Extra_info(){
  3. return "Adding info from Extra_info";
  4. }
  5. void Animal:: Print(void){
  6. cout << name << Endl;
  7. cout << Extra_info() << Endl;
  8. }


Main.cpp is of course the user of this class.


Click ( here) to collapse or open

  1. #include "Animal.h"
  2. int main(void){
  3. Animal ani("any");
  4. Ani. Print();
  5. cout << "some" << Endl;
  6. }

Compile


Click ( here) to collapse or open

  1. G++ animal. cpp main. cpp

Run


Click ( here) to collapse or open

  1. ./a.out

Output


Click ( here) to collapse or open

  1. Any
  2. Adding info from Extra_info
  3. Some

Well, three files, a class of the head, a class implementation, a user
Good luck

Simple organization of C + + multi-source files

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.