Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lab 1 Introduction to C++.

Similar presentations


Presentation on theme: "Lab 1 Introduction to C++."— Presentation transcript:

1 Lab 1 Introduction to C++

2 Microsoft visual studio.
In this lab, you will learn:  How to create a C++ source code using Microsoft visual studio.  How to compile and run use if/ else. use loops. C++ source code.  Function declaration definition call  How functions work

3 You can use Visual C++ in the Visual Studio
integrated development environment (IDE) to create Standard C++ programs. By following these steps in this example, you can create a project, add a new file to the project, modify the file to add C++ code, and then compile and run the program by using Visual Studio.

4 1. Select "File" menu ⇒ "New" ⇒ "Project...".
2. In the "Project types" box select "VC++" and in templates box select "Win32 Console Application“ Check the "Create directory for solution" box 3. 4. In the "Name" field, enter "Hello" as the project name 5. In the "Location" field, choose your working directory 6. Click "OK“ See the following figure

5

6 7. "Win32 Application Wizard" dialog-box
appears ⇒ Click "Next"

7 8. In "Application settings", select “Empty project”
9. Click "Finish"

8 project node and right-click on "Sources Files"
1. In "Solution Explorer" panel, expand the "Hello" project node and right-click on "Sources Files" ⇒ "Add" ⇒ "New Item..."

9 "Categories" box, select "Code"
2. In 3. In 4. In the the the "Categories" box, select "Code" "Templates" box, select "VC++ File (.cpp)" "Name" field, enter "Hello" as the filename 5. Click "Add"

10 #include <iostream> using namespace std; int main()
6. In the editor panel for "Hello.cpp", type the following C++ codes #include <iostream> using namespace std; int main() { cout << "Hello, world!" << endl; }

11

12 From the "Build" menu, choose "Build
Solution" (or hit F7 key) to compile and link the program. Build results will be shown in the output panel

13 From the "Debug" menu, choose "Start Without
Debugging" (or click Ctrl+F5). You should see the message "Hello, world!" appears on the console

14 #include <iostream>
using namespace std; int main() { int marks; cout << "Enter your marks" << endl; cin >> marks; if (marks > 50) cout << "You are passed!" << endl; else cout << "You are failed"; return 0; }

15 #include <iostream>
using namespace std; int main() { int course_count=1, marks; while (course_count <=5) { cout << "Enter your marks for course# " << course_count << endl; cin >> marks; if (marks > 50) cout << "You are passed in course# " << course_count << endl; else cout << "You are failed in course# " << course_count << endl; course_count = course_count+1; } return 0;

16 #include <iostream>
using namespace std; int main() { int count, num,total=0; for (count=0;count<5;count++) cout << "Enter number" << endl; cin >> num; total=total+num; } cout << "The total number =" << total << endl; return 0;

17

18 return_type function_name (parameter list);
A function declaration is made by declaring the return type of the function, name of the function and the data types of the parameters of the function. A function declaration is same as the declaration of the variable. The function declaration is always terminated by the semicolon. A call to the function cannot be made unless it is declared. The general form of the declaration is: return_type function_name (parameter list);

19 The basic form of a function definition is like this:
A function definition contains a function declaration and the body of a function. It describes how the function performs the task or computation and returns the specified values if required. The basic form of a function definition is like this: return_type function_name(parameter list) { // code to execute inside function }

20 function_name(parameter list);
A function call is an expression containing name of the function and a parenthesized argument list. The basic form of a function call is like this: function_name(parameter list);

21 #include <iostream>
using namespace std; double rectArea (double l, double w); int main() { double length, width, area; // This program calculates the area of a rectangle cout << "Enter the length of the rectangle: "; cin >> length; cout << "Enter the width of the rectangle: "; cin >> width; area = rectArea(length, width); cout << "The area of the rectangle is " << area << endl; return 0; } double rectArea (double l, double w) { return l * w; }

22 Write a complete C++ program that has three
functions named as follows: I. int square(int, int) - calculate an area of square which the arguments are length and width (length*width). II. double triangle(double, double) – calculate an area of triangle which the arguments are height and base (0.5*height*base). III. double circle(double) – circle ( * (radius * argument is radius. Your program is terminated calculate an area of radius)) which the when required by user.

23 The input/output should be look like below:
Calculate an area of: 1. Square 2. Triangle 3. Circle Please give your choice [1/2/3]: 2 Enter height and base : 5 10 The area of triangle is 25.0 Continue [y/n]: y Please give your choice [1/2/3]: 1 Enter height and base : 10 10 The area of triangle is 100 Continue [y/n]: n Thank you!

24 #include <iostream>
using namespace std; int square(int, int); double triangle(double, double); double circle(double); int square(int sq_h, int sq_w) { return sq_h * sq_w; } double triangle(double h, double b) { return 0.5 * h * b; } double circle(double r) { return * (r * r); }

25 int main() { int sq_height, sq_width, choice; double height, base, radius; char ch; do cout << "Calculate an area of: " << endl; cout<<" 1.Square”<<endl; cout<<" 2. Triangle" << endl; cout<< " 3.Circle“<<endl; cout<<endl << " Please give your choice [1/2/3]: " ; cin>>choice; cout << endl;

26 switch (choice) { case 1 : cout<<" Enter height and width:"; cin>>sq_height>>sq_width; cout<<endl <<" The area of square is " << square(sq_height, sq_width); break; case 2: cout<<" Enter height and base:"; cin>>height>>base; cout<<endl <<" The area of triangle is " << triangle(height, base); case 3: cout<<" Enter radius:"; cin>>radius; cout<<endl <<" The area of circle is " << circle(radius); default: cout<<"Invalid choice!"; } cout<< endl <<"Continue [y/n] :"; cin>>ch; cout<<endl; } while (ch == 'y' || ch == 'Y'); cout << "Thank you!"; system("pause"); return 0;}

27 Write a C++ program with function
qualityPoints that inputs a student’s average and returns 4 if a student’s average is 90- 100, 3 if the average is 80-89, 2 if the average is 70-79, 1 if the average is 60-69, and 0 if the average is lower than 60.


Download ppt "Lab 1 Introduction to C++."

Similar presentations


Ads by Google