1 / 16

Mech 215 Tutorial 1

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 ; .

rae
Download Presentation

Mech 215 Tutorial 1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Mech 215Tutorial 1 By:- ShrutiRathee

  2. 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.

  3. 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.

  4. 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

  5. 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.

  6. 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; }

  7. 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.

  8. 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; }

  9. 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.

  10. 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

  11. 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.

  12. 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.

  13. 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, . . }

  14. 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.

  15. StructsDatatype • C++ has a struct declaration which is useful to hold information of different data types pertaining to a certain object or thing.

  16. 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

More Related