1
Cal Poly Pomona
Electrical & Computer Engineering
Introduction to Visual C++.NET
Introduction to Visual C++.NET
Programming
Programming
ECE 114 - 2
Dr. Z. Aliyazicioglu
Cal Poly Pomona
Electrical & Computer Engineering
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 3 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
Double click the your cpp file under Solution Explore
(on the right side)
Cal Poly Pomona ECE 114-1 5
A simple Program:Printing a Line of Text
A simple Program:Printing a Line of Text
// First Program in Visual C++.NET#include"stdafx.h"
#using<mscorlib.dll>
usingnamespace System;
int_tmain() {
// Display string
Console::WriteLine( S“Welcome to Visual C++.NET Programming!” );
return 0 ; }
// end _tmain
// indicates that the remainder of line is a comment
Directive, instruct the compiler to treat the comments of a specific file
Main is a program building block called a function Print on the screen the string of characters
Imports prepackaged unit of code, (dll file)
Declares the use of namespace in the program
Cal Poly Pomona ECE 114-1 7
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.
Console::WriteLine( S"Welcome to \nVisual C++.NET
\nProgramming!" );
Display Line of Text
Console::WriteLine( S"Welcome to" );
Cal Poly Pomona ECE 114-1 9
Some common escape Sequence
Alert. Sound the system \a
Double quote, prints (“) character \”
Backslash, prints (\) character \\
Carriage return, moves to beginning of current line
\r
Horizontal tab, move to next tab \t
Newline, moves to beginning of new line \n
Description
Escape
Sequence
Adding Integer
// An addition program that adds two integer // Z. Aliyazicoglu
#include "stdafx.h" #using <mscorlib.dll> using namespace System;
Enter two integer number typed by a user at the keyboard number1
number2 Compute the total
sum=number1+number2 Dislay the result
Source Code
Cal Poly Pomona ECE 114-1 11
continue
int_tmain() {
String *firstNumber, // First user input
*secondNumber; // Second user input
intnumber1, // First number.
number2, // Second number
sum; // sum of both number
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() ); return0;
}
Declarations
Result
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 13
#include "stdafx.h" #using <mscorlib.dll> using namespace System; int _tmain()
{
int number1, // First number.
number2, // Second number
sum; // 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;
Alternative source code
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 15
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 (;)
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 17
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. 80 sum 35 number2 45 number1
Memory
Location
Arithmetic Operations
R % x R mod x % Modulus x / y x / y / Division x * y xy * Multiplication P - c p - c -Subtraction F + 7 f + 7 + Addition MC++ expression Algebraic expression Arithmetic operation MC++ OperationCal Poly Pomona ECE 114-1 19
Precedence of Arithmetic Operators
Operators Operations Order of Evaluation
( ) Parentheses
Evaluate first. If there are several, evaluate left to right
*,/,or %
Multiplication Division Modulus
Evaluate second. If there are several evaluate left to right
+ or
-Addition Subtraction
Evaluate last. If there are several, evaluate left to right
Decision Making
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 x > y x < y x >= y x <= y > < >= <= relational operators > < ≥ ≤ x is equal to y x is not equal to y x = = y x != y = = != equality operator = ≠ Meaning of C++ condition Example of C++ condition C++ equality or relational operator Standard algebraic equality operator or relational operatorsCal Poly Pomona ECE 114-1 21
Data Types in .NET Framework
String * Fixed-length string of char
String
wcher_t or _wchar_t Unicode (16-bit) character
Char
bool Boolean value (true and false)
Boolean
double Double-precision(64-bits)
floating point number Double
float Single-precision(32-bits)
floating point number Single
_int64 64-bits signed integer
Int64
int or long 32-bits signed integer
Int32
short 16-bits signed integer
Int16 MC++ Type Description FLC Structure Class name
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 = π r2
circumference = 2 π r 3- Get circle radius
Calculate area
Calculate circumference
Cal Poly Pomona ECE 114-1 23 // 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 () );
/* 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;
}
Source Code
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 25
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