Вы находитесь на странице: 1из 10

C++ FOR BEGINNERS

CHAPTER 1 : THE INTRODUCTION

BY: MOHAMAD AMIR ZAIM BIN MOHAMAD ZAINI


2010693576 CS110 DIPLOMA OF COMPUTER SCIENCE UNIVERSITI TEKNOLOGI MARA (UiTM) KAMPUS ARAU, PERLIS, MALAYSIA 2012

DISCLAIMER
This guide is intended for open, public and non-commercial purpose only. As the reader, you can share the guide with your family and friends and also the people worldwide. Also, you can make copies of this guide as many as you can, but NEVER MAKE ANY ATTEMPT TO TAKE ADVANTAGE OF ONE'S WORK BY ATTEMPT TO COMMERCIALIZE OWNER'S OWN WORK WITHOUT THE PERMISSION FROM THE OWNER!

ABOUT THE AUTHOR...

See my picture above, that is me...My name is Mohamad Amir Zaim Bin Mohamad Zaini and I'm lived in the state of Penang, Malaysia. I'm currently studied in Universiti Teknologi Mara (UiTM) or called as Mara University of Technology for international reference and my campus is located at the town of Arau, state of Perlis, Malaysia. I am a student of Diploma of Computer Science and I'm love about computers and stuffs since I'm was a kid ago. Reason why I'm being so much love about the computer stuff? Because I'm want to create something new and also other than usual. Not just I'm working as a student-class programmer, but I'm just being worked to fix somebody pc/laptop that have to need a charge of fixing these. So, that's my short bio about me...Nothing more, nothing less...

PREFACE
Ok, this is my first guide of programming in c++ language that helps every people around the world to learn the C++ programming language with easy and effective. C++ is the mostwidely programming language around the world and running on most of the computers nowadays. C++ was created by Bjarne Stroustrup in 1979 and originally it was called as C with classes, but later he turned the name of the programming language into c++ in 1983. Later, new programming language are born that are based from C++ such as Java and so on. In this guide, I will explain the basic thing of C++ programming from the beginner level to the intermediate level ones. Hope you can learn some new stuffs in the programming world. For learning the C++, you will required a compiler that reads the source code and then translates the codes into the object codes where the computer understands. There are many compiler softwares that you can find it in the internet. For me, I would recommended to use Code::Blocks, which is the open-source compiler and using GNU GCC/C++ compiler as default compiler.

CHAPTER 1: THE INTRODUCTION


Have you ever looked at the program? Most people would looked at the program as the executable file which operated the tasks that are specified to the user. But how are these inside body of the programs are look like? Well, I'm show to you the example of the source code of the program that are based from c++: #include <iostream> using namespace std; int main() { cout << Hello world! Welcome to C++! << endl; return 0; } Well, this is the example of the source code of the program. Source code can be either open-source or closed-source. Open-source source code means that your source code can be distributed or modified with freedom. Meanwhile closed-source means that only the owner/company that developing the softwares can able to see the code and it cannot be distributed to the public or modified by the public. > 1.1 BASIC STUFF OF C++ Before that, I'm showing the example of the source code above and that source code is called as C++ programming language source code. Here, I'm showed to you many things that are included in c++ //--+ 1.1.1 STRUCTURE OF C++ The c++ structure consist of head of program and also the main body of the program. Subprograms that are located outside the main body are called as functions, but I'm will discuss it in next chapter later. Here are the structure: #include<iostream> //header using namespace std; int main() { //program statement here return 0; } Header is the important thing in the c++ source code where it loads the predefined calls into the program. <iostream> header enables the program to perform I/O operation,

<cstring> header lets you to handle string processings, <cmath> header performs mathemathical operation and so on. Headers are vital in the programming and also, you can also create your own header or user-defined headers. In the body of main program, you can put anything of programming syntax here, as long as it follow the flow of program processing and also have the correct syntax and right datatypes that you used. //--+ 1.1.2 DATATYPES OF C++ Data type can be defined as a set of values together with a set of operation. There are many types of C++ data types: >Numerical-based data types. >String-based data types. (i)<--< Numerical-based datatypes: int Used to handle integer values either +ve/-ve long same function as int, but it holds more value compared to int float Holds the floating-point values (decimal points) either +ve/-ve double Same function as float, but holds more value than float bool Holds only a single value in binary form, 0 (false) and 1 (true) Example: int a; float num1; long distance = 10000; double totalSales=0; bool check=true; (ii)<--< String-based datatypes: char Used to hold string values either alphabets, symbols and so on. Example: char answer; //allows only input single character char name[30]; /*allows to input multiple character but not exceeding the maximum character */ char value1[30] = Programming; //defining the string value

//--+ 1.1.2 INPUT/OUTPUT OPERATION OF C++ Here, in C++, you will need to know the basic syntax that involves input/output operation: cin input the character from the keyboard; cout display the output. Any line of syntax must be end with semicolon (;), otherwise the compiler will produce an error. Example: cin >> value1; character */ /*inserting the value either integer of single

cin.get(studName,30); /* inserting the combination of strings, but not exceeding max char /* cin.ignore(80,'\n'); /* ignoring the rest of character space and then making a new line */ cout << This is C++ << endl; /* displaying a text then 'endl' is used for making a new line */ cout << value1 << endl; //displaying the value of the datatype //--+ 1.1.3 ARITHMETIC & MATHEMATICAL OPERATION OF C++ In c++, you can perform arithmetical and mathematical operation. Here are the example: + addition - subtraction * multiplication / division % modulus, used to find the remaining value of the division of 2 numbers Example: sum = a + b; average = (x+y+z)/3; z = (x(y+z))%3; Also, you can use header <cmath> to use the some of the mathematical functions such as pow(x,y), sqrt(x,y) and so on.

//--+ 1.1.4 CONSTANT VALUES Constant values can be defined and it cannot be changed during its execution process. Example: const dataType variable = value; const float PI= 3.142; const int vrx = 23; //--+ 1.1.5 MISCHELLANOUS FEATURES IN C++ a> Reserved words Reserved words or keywords are the words that you cannot be used as the variable for a values or anything other than its function for these words. These included data types, void, return, const and so on. b> Special symbols Special symbols are often used in arithmetical expressions and logical expressions. These including: + - * / % . ; ? , <= != == >= //this symbol will be explained in chapter 2 later c> Comments Sometimes while you are coding the program, it is better if you are explaining what are you writing these codes. //this is the example of comment when it is started with // every line /* you can create comments with multiple lines started with '/*' and ends with '*/' */ These comments will be ignored by the compiler.

> 1.2 C++ SOURCE CODE EXAMPLES a) #include<iostream> using namespace std; int main() { int num1, num2, sum; float avg; char exit; /*will only exiting the program after user input char and press enter to exit */ cout << Enter the first number: ; cin >> num1; cout << Enter the second number:; cin >> num2; sum = num1+ num2; cout << Sum of 2 numbers: << sum << endl; avg = sum/2; cout << Average of 2 numbers: << avg << endl; cout << Press any key to exit << endl; cin.get(exit); return 0; } b) #include<iostream> using namespace std; int main() { char name[30]; char exit; cout << Please enter your name: ;

cin.get(name,30); cin.ignore(80,'\n'); cout << Hello, << name << ! << endl; cout << Press any key to exit. << endl; cin.get(exit); return 0; } c) #include<iostream> #include<cmath> using namespace std; const float PI= 3.142; int main() { int radius; double circumference, area; char exit; cout << Enter the radius value of a circle:; cin >> radius; circumference = 2*PI*radius; area = PI*pow(radius,2); cout << Area of circle: << area << endl; cout << Circumference of circle: << circumference << endl; cout << Press any key to exit. << endl; cin.get(exit); return 0; }

TUTORIAL QUESTION: 1. 2. 3. 4. What is a data type which allows to hold integer values? What is the function of data type char? What is the type of header that used for mathematical functions? Create a program code that prompt user to input their names and the display is Hello username. Nice to meet you! 5. Create a program code that prompts users to input 3 numbers and finding the sum of all numbers. Then subtract the sum with the first number that you had been input.

----------------------END OF THE CHAPTER----------------------------REFERENCE: D.S Malik, C++ Programming: From Problem Analysis to Program Design, International Edition, Course Technology, Cengage Learning, 2011.

Вам также может понравиться