160 likes | 299 Views
Mech 215 Tutorial 1. By:- Shruti Rathee. Basics of C++. Our first program is a simple hello program # include < iostream > // This is a comment // /* This is old style comment*/ using namespace std ; .
E N D
Mech 215Tutorial 1 By:- ShrutiRathee
Basics of C++ • Our first program is a simple hello program • #include <iostream> // This is a comment // /* This is old style comment*/ using namespace std; This is preprocessor directive to tell compiler to include iostream. Iostream is used to support console input/output. It will tell the compiler to find the names in standard library. Names like cout and endl are in standard library.
Every c++program is executed from main function. “;” this is the statement terminator. Cout is console output “<<” it is stream insertion operator and it send a string to the console. Endl is end line. int main() { cout << "Hello World" << endl; return 0; } It is used to exit the main program. “0” indicates that program has terminated successfully.
How to run this program • To compile a file type this:- g++ nameofprogram.cpp -0 hello This will create an executable file named hello and you can run that by using :- • . /hello
What if you want to take input from keyboard • To do this we use : cin >> Cin is console input and is used to read a value from the keyboard. >> it is referred to as steam extraction operator and assigns an input to a variable.
Example program for cin >> • #include <iostream> using namespace std; int main() { inti,sum; cout<< “enter value for i" ; cin >>i; sum = i+3; cout<< “The sum is" << sum<<endl; return 0; }
Identifiers • The names of the things that appear in program is known as identifier. • It must start with a letter or an underscore. It cannot start with a digit. • An identifier cant be the same name of reserved keywords such as else, enum,do,double.
Example : Here ``i`` and ``sum`` are the identifiers. • #include <iostream> using namespace std; int main() { inti,sum; cout<< “enter value for i" ; cin>>i; cout<< “enter value for sum" ; cin>>sum; cout<< “The value for i is" <<i<<endl; cout<< “The value for sum is" <<sum<<endl; return 0; }
Variables • These are used to store the values that can be used later in the program. • They are called variables as their value can be changed.
Example : • #include <iostream> using namespace std; intmain() { inti; i=10; cout<< “i = " <<i<<endl; i=5; cout<< “i = " <<i<<endl; return 0; } Here the output will be something like : i=10 i=5
Referance Variables • Reference variables allow two variable names to address the same memory location. • It is declared using & operator. • A reference variable must always be initialized.
Example : • #include <iostream> using namespace std; main() { intvar1; int &var2 = var1; var1 = 10; cout<< "var1 = " << var1 <<endl; cout<< "var2 = " << var2 << endl; return 0; } var2 is a reference variable.
EnumDatatype • Enumerations create new data types to contain something different that is not limited to the values fundamental data types. • enumname_you_want { value1, value2, value3, . . }
Example : • enum Color { COLOR_BLACK, // assigned 0 COLOR_RED, // assigned 1 COLOR_BLUE, // assigned 2 COLOR_GREEN, // assigned 3 COLOR_WHITE, // assigned 4 COLOR_CYAN, // assigned 5 COLOR_YELLOW, // assigned 6 COLOR_MAGENTA // assigned 7 }; Color eColor = COLOR_WHITE; cout << eColor; ``http://www.learncpp.com/cpp-tutorial/45-enumerated-types/`` The cout statement above prints the value 4.
StructsDatatype • C++ has a struct declaration which is useful to hold information of different data types pertaining to a certain object or thing.
Example : • #include <iostream> #include <string> using namespace std; structfruit{ string name; String taste; }; int main(){ fruit f; f.name = “mango"; f.taste = “tangy" ; cout<< "Name: " << f.name << endl; cout<< “Taste: " << f.taste << endl; return 0; } It will output the contents of the fruit`s struct