[C++] โครงสร้างเบื้องต้น


โปรแกรมที่ใช้ Dev C++

C++ supports 
  • the concepts of OOP: data abstraction, data encapsulation, inheritance, polymorphism
  • Extensions: exception handling, templates
ตัวอย่าง

Ex1 Basic structure

#include <iostream>
using namespace std;
int main()
{
 cout << "Enjoy yourself with C++!" << endl;
 return 0;
}

คำอธิบาย
  • # indicates that the line is intended for the preprocessor.
  • #include <filename> allows the program access to all the information contained in the header file.
  • Predefined names in C++ are to be found in the std (standard) namespace. The using directive allows direct access to the names of the std namespace.
  • The name cout (console output) designates an object responsible for output.
  • <<, characters are being “pushed” to the output stream. endl (end of line) causes a line feed.
  • returning a value of 0 as an exit code to the calling program.

Ex2 Prototype (Function)

#include <iostream>
using namespace std;

void line(), message(); // Prototypes
int main() {
 cout << "Hello! The program starts in main()." << endl;
 line();
 message();
 line();
 cout << "At the end of main()." << endl;
 return 0;
}
void line() {
 cout << "--------------------------------" << endl;
}
void message() {
 cout << "In function message()." << endl;
}

Previous
Next Post »