![]() |
|
C++ basics part 1 - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C) +--- Thread: C++ basics part 1 (/Thread-C-basics-part-1) Pages:
1
2
|
C++ basics part 1 - ICE_ - 06-18-2013 Before I begin i'd like to say this is 100% MINE i am just transferring it from SF Hey all, here is a basics tutorial for c++: _________________________________ The basic syntax c++ may seem like a scary language but in reality, its just like any other programming language, it has a main function and a proper syntax. For example, the most basic program in c++ is: Code: #include <iostream>
using namespace std;
int main()
{
return 0;
}see! not so bad Laugh... Lets look at this piece by piece, Code: #include <iostream>Code: using namespace std;std is the standard 'namespace' in c++, this is also used in every program so that the basic functions of a program can be performed. Code: int main()
{
return 0;
}this is the main method, in java this would be public static void main(String[] args). As you can see, this function uses brackets to show where the code should go, these are called body-brackets, all your code should be inside the body brackets of functions.In addition, you can see the 'return 0;' inside this code brackets, this tells the compiler that whenever it gets to there that the code is finished and it should terminate the program with status report '0'. a ';' is placed at the end of the line to tell the compiler that there will be no more code on that line. The return statement IS NOT NEEDED in all programs. _________________________________ Hello World! c++ is like any normal programming language, it has a printing function, an input function, arrays, lists, etc. However, the first thing we will be learning is the printing function for c++: Code: #include <iostream>
using namespace std;
int main()
{
cout << "Hello World!!"<<endl;
}the above code will print out "Hello World!!". In this line of code, 'cout' means "console-out" and << is telling the compiler that after the print statement (cout), text will need to be read. Futhermore, "endl" means 'end line' which creates a new line, you can also do this in the line of code with '\n': Code: Code: #include <iostream>
using namespace std;
int main()
{
cout << "Hello World!!\n";
}both will come out with the following in your command prompt: uoirghjeiogjwepigAutoGeneratedCommandPromptText4ryghurgijrg Hello World!! press any key to continue... _________________________________ comments Code: #include <iostream>
using namespace std;
int main()
{
//this is a one line comment
/* This
* Is
* A
* Multi
* Line
Comment */
}A comment is a line within the code that the compiler does not read, it is made for the creator and any other people who should happen to see the source code. Comments are mainly used to explain what a line of code/ block of code does. For Example: Code: #include <iostream>
using namespace std;
int main()
{
cout << "Hello World!!";// this will print out hello world
/* and it will look like this:
* Hello World!!
*/
}variables Just like every programming language, c++ has some basic variables these are: string - a line of text int - a whole number double - a number with decimals float - an extremly long number with decimals char - a single charachter, such as 'a' or '!' All of these can be used together in the following way: Code: #include <iostream>
using namespace std;
int main()
{
int age = 15;
double time = 7.59;
string name = "Daniel";
char exclamation = '!';
cout << "Hello, my name is " <<name<<" and I am "<<age<< " and it is "<<time<< " o'clock"<<exclamation<<'\n';
}Code: Hello, my name is Daniel and I am 15 and it is 7.59 o'clock! this is because, we used the << operator to append (add) all the variables we created to the original string of text in front of the cout statement. _________________________________ math like anything in life, math is a large part of programming. In c++ the math operators are: + || addition - || subtraction * || multiplication / || division % || get the remainder of something < || less then > || greater than <= || less then or equal to >= || greater than or equal to this can used to make a simple calculator: Code: #include <iostream>
using namespace std;
int main()
{
int add1 = 1;
int add2 = 2;
int mult1 = 5;
int mult2 = 5;
int div1 = 6;
int div2 = 3;
int rem1 = 12;
int rem2 = 4;
int sum = add1 + add2;
int multans = mult1 * mult2;
int divans = div1 / div2;
int remans = rem1 % rem2;
cout << sum<<endl;//3
cout << multans<<endl;//25
cout << divans<<endl;//2
cout << remans<<endl;//0
}you can also do this within the print statement: Code: #include <iostream>
using namespace std;
int main()
{
int add1 = 1;
int add2 = 2;
int mult1 = 5;
int mult2 = 5;
int div1 = 6;
int div2 = 3;
int rem1 = 12;
int rem2 = 4;
cout << "sum is "<< add1 + add2<<endl;
cout << "multans is "<< mult1 * mult2<<endl;
cout << "divans is "<< div1 / div2<<endl;
cout << "remans is "<< rem1 % rem2<<endl;
cout <<"1+1 is "<<1 + 1<<endl;
cout <<"2*2 is "<<2 * 2<<endl;
cout <<"4/2 is "<<4 / 2<<endl;
cout <<"12%5 is "<<12 % 5<<endl;
}user input User input is basically the user entering a string or a number and it/them being set as integers to be used in another time. To do this we use the 'cin' operator which means console-in. Code: #include <iostream>
using namespace std;
int main()
{
string name;
cout << "Enter name: ";
cin >> name;
cout << "Hello "<<name;
}this program will print out whatever name you specified. As you can see, instead of << like we use in cout, for cin we use >> and then the variable name. _________________________________ calculator Now we will be making a basic addition calculator program with user input. Code: #include <iostream>
using namespace std;
int main()
{
int firstnum;
int secondnum;
cout << "enter first number: ";
cin >> firstnum;
cout << "enter second number: ";
cin >> secondnum;
int sum = firstnum + secondnum;
cout << "the sum of "<<firstnum<<" and "<<secondnum<< " is "<<sum<<endl;
}That is all for part 1, in part 2 I will be covering loops, if statements and recursion. I hope you liked this tutorial! RE: C++ basics part 1 - Nil - 06-18-2013 Holy hell! Who created these languages and why do they have to be like this? It looked like a good guide anyway if I knew what I was reading. RE: C++ basics part 1 - w00t - 06-18-2013 It's better than the lower levels. Oh, you want to print "hi" in ASM on Windows? Code: _start:
push -11
call GetStdHandle
push 0x0
push 0x0
push 5
push msg
push eax
call WriteConsole
msg:
db "Hi", 0xa, 0xd, 0x0RE: C++ basics part 1 - Charon - 06-18-2013 Nice tutorial, Keep it up. And indeed low level programing languages such as ASM are a lot more complex than C++ in my opinion. RE: C++ basics part 1 - w00t - 06-18-2013 I wouldn't say more complex, because it's fairly straightforward what every operation does, but certainly more tedious. RE: C++ basics part 1 - Dong - 09-14-2013 Fair tutorial, however it could prove to be more useful to new programmers if you'd explain things better. For example, Quote: You neglect to explain what a namespace actually is or what the 'using' keyword actually indicates to the compiler. In addition to this, to access the functions in the 'std' namespace, one does not have to declare 'using namespace std'. You can access the functions, objects and whatnot simply by doing this: Code: std::string
OR
std::cout << "LOL";
OR
int x=0;
std::cin >> x;
etc.I just feel as if that is worthy of a small mention, at least. Also, Quote:Just like every programming language, c++ has some basic variables these are: You should not have mentioned the string object before getting to the subject of arrays (since a string is basically an array of characters). It would have been a lot better for the new programmers to learn about characters, arrays then arrays of characters before making use of C++'s string class. In addition to that, there are a number of differences in functionality between C-style strings and C++ style strings. These differences are very important for the readers to understand and become aware of. C-style string: Code: char hello[6]={'h', 'e', 'l', 'l', 'o', '\0'};
OR
char hello[6]="hello";Making use of the C++ string class: Code: In order to use C++ style strings, one must include this class (it can be found in the standard C++ library):
#include <string>
Upon doing that, you can declare C++ style strings using the keyword "string":
string hello="hello";Also, you probably should have mentioned the range and size of each basic data type. Probably throw in some better descriptions as well... Just a very simple example of what I think you should have displayed to the newbie programmers: Quote:char - This data type can hold a single character of size 1 byte. I'm not going to list all of the basic data types that a beginner should be familiar with, so I'll direct anyone reading this to a couple of useful links: Code: http://www.tutorialspoint.com/cplusplus/cpp_data_types.htm
http://www.cplusplus.com/doc/tutorial/variables/In conclusion, I found the tutorial was pretty alright. Just required a lot more explanation. Not only that, but by making a basic C++ tutorial you should always assume that your readers are complete beginners unless you specify otherwise. RE: C++ basics part 1 - Ghost - 09-14-2013 I like it i might want to learn coding ill keep this tutorial in mind! RE: C++ basics part 1 - Cyanide and Cynicism - 09-14-2013 (09-14-2013, 12:30 AM)Aristotle Wrote: -snip- Beat me to it. Also, what the fuck are these comments? Code: /* This
* Is
* A
* Multi
* Line
Comment */Use something like: Code: /* This
* is
* a
* good
* long
* comment
*/Or: Code: // This
// is
// a
// good
// non-conventional
// long
// commentRE: C++ basics part 1 - cxS - 09-19-2013 Quote: Ahh, no... :S Basic functions of a program, printf, if statements, input, etc...? It has nothing to do with if statements. Quote:string - a line of text Is this implying that float is used for larger precision than double??? And string, a line of text? :S (09-14-2013, 12:37 AM)Cyanide and Cynicism Wrote: Beat me to it. What does it matter? You're doing the same thing as what he wrote, and the first qualifies as a comment just as much as the double slash variation, it's preference. RE: C++ basics part 1 - Cyanide and Cynicism - 09-19-2013 (09-19-2013, 03:15 AM)cxS Wrote: What does it matter? You're doing the same thing as what he wrote, and the first qualifies as a comment just as much as the double slash variation, it's preference. Because it looks gross. |