ECE 114-2 Introduction to Visual C++.NET Programming Dr. Z. Aliyazicioglu Cal Poly Pomona Electrical & Computer Engineering Cal Poly Pomona Electrical & Computer Engineering 1 Using.NET Environment Start up Microsoft Visual studio.net. The following window should be displayed Click New Project or Under the File menu, point to New, and then click Project Cal Poly Pomona ECE 114-1 2 1
New Project dialog box will be displayed Select Visual C++ Project in the Project Types pane, Select Console Application (.NET) in the temples pane. Type project Name in Name box Choose location for your project. Then, Click OK Cal Poly Pomona ECE 114-1 3 Double click the your cpp file under Solution Explore (on the right side) Cal Poly Pomona ECE 114-1 4 2
Cal Poly Pomona ECE 114-1 5 A simple Program:Printing a Line of Text // First Program in Visual C++.NET #include "stdafx.h" #using <mscorlib.dll> using namespace System; // indicates that the remainder of line is a comment Directive, instruct the compiler to treat the comments of a specific file Imports prepackaged unit of code, (dll file) Declares the use of namespace in the program int _tmain() Main is a program building block called a function { // Display string Print on the screen the string of characters Console::WriteLine( S Welcome to Visual C++.NET Programming! ); return 0 ; } // end _tmain S prefix indicates that string literal follows MC++) Cal Poly Pomona ECE 114-1 6 3
Notes about the simple program comment line // indicates that the remainder of each line is a command. That is a documentation for programmer or other people to understand your program easily. Comments are ignored by the Visual C++.NET compiler. // is called a single line comment The command for many line: begin with /* and and with */. Anything between /* and */ will be comment. Cal Poly Pomona ECE 114-1 7 Display Line of Text Console::WriteLine( S"Welcome to" ); Console::WriteLine( S"Visual C++.NET Programming!" ); Console::WriteLine( S"Welcome to \nvisual C++.NET \nprogramming!" ); Cal Poly Pomona ECE 114-1 8 4
Some common escape Sequence Escape Sequence \n \t \r \\ \ \a Description Newline, moves to beginning of new line Horizontal tab, move to next tab Carriage return, moves to beginning of current line Backslash, prints (\) character Double quote, prints ( ) character Alert. Sound the system Cal Poly Pomona ECE 114-1 9 Adding Integer Enter two integer number typed by a user at the keyboard number1 number2 Compute the total sum=number1+number2 Dislay the result Source Code // An addition program that adds two integer // Z. Aliyazicoglu #include "stdafx.h" #using <mscorlib.dll> using namespace System; (Next slide) Cal Poly Pomona ECE 114-1 10 5
int _tmain() { String *firstnumber, *secondnumber; int number1, number2, sum; continue // First user input // Second user input // First number. // Second number // sum of both number Declarations Console::Write( S"Please enter the first integer : " ); firstnumber = Console::ReadLine (); Console::Write( S"Please enter the second integer : " ); secondnumber = Console::ReadLine (); //Convert numbers from type string * to type integer number1 = Int32::Parse( firstnumber ); number2 = Int32::Parse( secondnumber ); sum=number1+number2; Console::WriteLine( S"\nThe sum is {0}.",sum.ToString() ); return 0; Cal Poly Pomona ECE 114-1 11 } Result Combining the input and conversion operations Console::Write( S"Please enter the first integer : " ); number1 = Int32::Parse(Console::ReadLine () ); Console::Write( S"Please enter the second integer : " ); number2 = Int32::Parse(Console::ReadLine () ); Cal Poly Pomona ECE 114-1 12 6
#include "stdafx.h" #using <mscorlib.dll> Alternative source code using namespace System; int _tmain() { int number1, number2, sum; // First number. // Second number // sum of both number Console::Write( S"Please enter the first integer : " ); number1 = Int32::Parse(Console::ReadLine () ); Console::Write( S"Please enter the second integer : " ); number2 = Int32::Parse(Console::ReadLine () ); sum=number1+number2; Console::WriteLine( S"\nThe sum is {0}.",sum.ToString() ); return 0; Cal Poly Pomona ECE 114-1 13 Declaration Types already defined in the.net Framework Some Primitive types» int, for integer numbers» float, double real numbers» _wchart_t character data Cal Poly Pomona ECE 114-1 14 7
Variable Name Can be any valid identifier Series of characters Underscore Start with character Case sensitive No keyword Example: Name7, _number, name_lastname Declaration separated by a comma and end with semicolon (;) Cal Poly Pomona ECE 114-1 15 Display the result Console::WriteLine( S"\nThe sum is {0}.",sum.ToString() ); Obtain the string representation of variable sum using method ToString. Use {0} to indicate a placeholder for variable values Console::WriteLine( "The number entered are {0} and {1}.,number1.ToString(), number2.tostring() ); The value of number1.tostring() would replace {0} The value of number2.tostring() would replace {1} Cal Poly Pomona ECE 114-1 16 8
Memory Display number1 = Int32::Parse( firstnumber ); number2 = Int32::Parse( secondnumber ); Convert the string to int int is placed into a memory location assigned for number1 and number2 by the compiler sum=number1+number2; Performs the addition and replaces sum s previous value in the memory. number1 number2 sum 45 35 80 Memory Location Cal Poly Pomona ECE 114-1 17 Arithmetic Operations MC++ Operation Arithmetic operation Algebraic expression MC++ expression Addition + f + 7 F + 7 Subtraction - p - c P - c Multiplication * xy x * y Division / x / y x / y Modulus % R mod x R % x Cal Poly Pomona ECE 114-1 18 9
Precedence of Arithmetic Operators Operators Operations Order of Evaluation ( ) Parentheses Evaluate first. If there are several, evaluate left to right *,/,or % + or - Multiplication Division Modulus Addition Subtraction Evaluate second. If there are several evaluate left to right Evaluate last. If there are several, evaluate left to right Cal Poly Pomona ECE 114-1 19 Standard algebraic equality operator or relational operators Decision Making C++ equality or relational operator Example of C++ condition Meaning of C++ condition equality operator = = =!= x = = y x!= y x is equal to y x is not equal to y relational operators > < > < >= <= x > y x < y x >= y x <= y x is greater than y x is less than y x is greater than or equal to y x is less than or equal to y Cal Poly Pomona ECE 114-1 20 10
Data Types in.net Framework FLC Structure Class name Int16 Int32 Int64 Single Double Boolean Char String Description 16-bits signed integer 32-bits signed integer 64-bits signed integer Single-precision(32-bits) floating point number Double-precision(64-bits) floating point number Boolean value (true and false) Unicode (16-bit) character Fixed-length string of char MC++ Type short int or long _int64 float double bool wcher_t or _wchar_t String * Cal Poly Pomona ECE 114-1 21 Case Study Finding the Area and circumference of a circle 1- Take the radius of a circle and compute and print its area and circumference 2- Inputs: Circle radius Outputs: Area of the circle Circumference of the circle Constants: PI = 3.14159 Formula: area = π r 2 circumference = 2 π r 3- Get circle radius Calculate area Calculate circumference Display area and circumference Cal Poly Pomona ECE 114-1 22 11
// Calculate and displays the area and circumference of a circle // Z. Aliyazicoglu #include "stdafx.h" #using <mscorlib.dll> using namespace System; #define PI 3.14159 int _tmain() { double radius, area=0.0, circum=0.0 ; /* Get the circle radius */ Console::Write( S"Please enter the radius : " ); radius = Double::Parse(Console::ReadLine () ); Source Code /* Calculate the area */ area = PI * radius * radius ; /* Calculate the circumference */ circum = 2 * PI * radius ; /* Display the area and circumference */ Console::WriteLine( S"\nThe area is {0}.",area.ToString() ); Console::WriteLine( S"\nThe circumference is {0}.",circum.ToString() ); return 0; } Cal Poly Pomona ECE 114-1 23 Homework #2 Problem 1 Write a program that inputs three integer from the keyboard and print the sum, average, product, smallest, and largest of these numbers. The screen dialogue should appear as fallow Input three different integers:13 27 14 Sum is 54 Average is 18 Product is 4914 Smallest is 13 Largest is 27 Cal Poly Pomona ECE 114-1 24 12
Homework #2 Problem 2 Using only the techniques you learned in this chapter, write a program that calculates the squares and cubes of the number from 0 to 10 and uses tabs to print the following table of values. Number Square Cube 0 0 0 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 Cal Poly Pomona ECE 114-1 25 13