SlideShare a Scribd company logo
1 of 4
Download to read offline
Development Software 2 ā€“ DOS200S
Compiled By WH. Olivier Ā©2013
Cape Peninsula University of Technology
Window Form Applications Using Visual C++
Variables, Constants and Calculations
Objectives and Learning Areas:
For this lesson you will learn how to do the follow:
1. Knowing the different data types
2. How to define constant and variable identifiers
3. The scope of identifiers and where to declare identifiers
4. How to convert data from one type to another.
Introduction
For any program that you will be developing in Visual C++, or any other computer language for that matter, has
three fundamental components to it. It is:
INPUT: what will be the input values that will be fed into the program by the user?
PROCESS: what are the operations to be performed by the program?
OUTPUT: what are the results that will be produced by the program as feedback to the user?
When designing an application program, the requirements for the application would state what the Inputs are, what
process must be performed on the input data, and what would be the output produced.
The components explained above are shown with the use of an application.
Storing and retrieving data
Data in the application above is stored in the Text property of the textBox object (control) on screen. For every
application that you develop, there must be a way to store and retrieve data. There are various ways in which to
store data and this is dependent on how you would want to use the data in the application. Here some way:
1. Constants. These are data types that are declared with a set value and cannot be changed throughout the
running of the application. A good example would be the number of months or number of days in a week.
const int WEEKDAYS = 7;
INPUTS
PROCESS
OUTPUT
INPUTS
Development Software 2 ā€“ DOS200S
Compiled By WH. Olivier Ā©2013
Cape Peninsula University of Technology
2. Variables. These identifiers are there to store data that are expected to change throughout the program.
When the user inputs different values, a calculation will produce different results for each input. Variables in
this case are best suited. An example would be a different price for a different item record.
double itemPrice;
3. Arrays. When you want to store a list or collection of related information, you can declare and array and
store the collection in it. Each individual item can them be reference via an index and using the same
identifier name. An example would be if you want to store a list of names or test scores.
int testScores[5];
4. Structures. This identifier is a user-defined data type which comprises of other data types and is designed to
suite the programmer needs. With this identifier, related variables are grouped into a single variable where
by each individual data member will be referenced using a access operator. An example would be related
student information like student number, name, subjects and scores.
struct StudDetail
{
int studNum;
string name;
string subjects[5];
};
Data Types and Usage
The table below shows the various data types that can be used in Visual C++. Also indicated are the managed and
unmanaged uses.
C++ Data
Type
Common Language
Runtime
(CLR) Data Types
(managed)
Primitive data
types
(unmanaged)
Use For Storage
Size in
Bytes
Sample data
Boolean Boolean bool True or False values 2 flag
Byte Byte - 0 ā€“ 255, binary data 1 singleNumber
Char - char Single Unicode character 2 Gender (M/F)
Date DateTime 1/1/0001 ā€“ 12/31/9999 8 currentDate
Decimal Decimal float Decimal fractions 16 Rand
Double Double double Double-precision floating-
point numbers
8 PI
Single Single Single-precision floating-
point numbers
4 Length
Short Int16 short Small integers 2 Age
Integer Int32 int Whole numbers 4 NumGoals
Long Int64 long Large whole numbers 8 IDnumber
String String^ string Alphanumeric data
(characters)
Varies Name
Object Object (class / struct) Any type of data 4
Development Software 2 ā€“ DOS200S
Compiled By WH. Olivier Ā©2013
Cape Peninsula University of Technology
Data declarations and scope of identifiers
Depending on where you declare an identifier, it can either be local or global. This means that if a variable is
declared within a function or procedure, it is local to that procedure or function. If, however, you would want that
identifier to be global, you would declare it outside of any function block.
Letā€™s look at the following example to illustrate local and global identifiers. A windows form application calculates
the gross salary by adding the basic salary to the bonus amounts. The calculation is done on the press of a button.
Local Variable Declarations
private: System::Void btnCalc_Click(System::Object^ sender, System::EventArgs^ e)
{
double basic = 0;
double bonus = 0;
basic = Double::Parse(txtBSalary->Text);
bonus = Double::Parse(txtBonus->Text);
double salary = basic + bonus;
txtGSalary->Text = "R " + salary.ToString("F");
}
Local variables can only be used within the function or procedure where it is declared in. This relates to the scope of
the identifier.
Global Variable Declarations
Global identifiers are not declared in any function or procedure. Their scope is global and therefore can be used in
any other function within the current form. There are two places in the code editor where global variables can be
declared.
The first area is in the ā€˜Required designer variableā€™ section. As shown below, it is just before the area where code for
all the controls on screen are generated.
/// <summary>
/// Required designer variable.
/// </summary>
Int32 intClickCounter;
Int32 intTimerCounter;
DateTime myTime;
#pragma region Windows Form Designer generated code
As the comment indicates, you can only declare variables in this area. Here you can use any data type which suites
the requirements.
The second area is just before the class definition of the current form as shown here below.
/// <summary>
/// Summary for Form1
/// </summary>
int globalNumber = 0;
public ref class Form1 : public System::Windows::Forms::Form
{...
Global variable declared in the ā€˜Require designer variableā€™ section cannot be initialized there. The best place to
initialize these variables is when the form is loaded as shown below.
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
intClickCounter = 0;
intTimerCounter = 30;
}
Local Variable declarations.
The variables are initialized to 0
Conversion from String to double.
The values on in the Text property
of the textbox control is converted
to double and assigned to the
double variables
Global Variable declarations. These
variables are declared in the
ā€˜Required designer variableā€™ section.
Global Variable declarations.
Declared just before the Form1 class
definition. It is also initialized here.
Global Variables Initialized.
Variables are initialized to 0
and 30 respectably.
Development Software 2 ā€“ DOS200S
Compiled By WH. Olivier Ā©2013
Cape Peninsula University of Technology
Constant declarations
Constants can either be global or local. When they are declared locally, they are declared with in a procedure or
function. There are two ways in which to define constants.
- Using the const reserve word.
-
- Using the #define preprocessor
#pragma once
const int MONTHS = 12;
#define LENGTH 10
#define WIDTH 5
namespace SalaryCalc1 {...
Take note of where constants are being declared above. These are declared as global constants.
Converting values from one data type to another.
When working with values where by the user must enter data in a textbox and that data must be used in some
calculation, these values must be converted to an equivalent type which would allow the calculation to be done.
Consider the following screen layout of Salary Calculation Application;
In this example the user is requested to enter the Basic Salary and Bonus
amounts. On pressing the ā€˜Calculate Salaryā€™ button, the application will take
the values in the textboxes as entered by the user and use that in a
calculation to produce the ā€˜Gross Salaryā€™. Note that values entered in on
screen are all regarded as Strings and can therefore not be used within a
calculation. For this reason, these values must first be converted to an
appropriate format which would allow the calculation to be done. Here is
the code that would accomplish this.
private: System::Void btnCalc_Click(System::Object^ sender, System::EventArgs^ e)
{
double basic = Double::Parse(txtBSalary->Text);
double bonus = Double::Parse(txtBonus->Text);
double salary = basic + bonus;
txtGSalary->Text = "R " + salary.ToString("F");
}
Constant named MOUNT defined using const
reserve word
Constants named LENGTH and WIDTH defined
using #define preprocessor.
Converting from
String to double
Converting from
double to String

More Related Content

What's hot

Book management system
Book management systemBook management system
Book management systemSHARDA SHARAN
Ā 
Chapter 05
Chapter 05Chapter 05
Chapter 05llmeade
Ā 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#Prasanna Kumar SM
Ā 
Chapter 3 ā€” Program Design and Coding
Chapter 3 ā€” Program Design and Coding Chapter 3 ā€” Program Design and Coding
Chapter 3 ā€” Program Design and Coding francopw
Ā 
Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4Mohd Harris Ahmad Jaal
Ā 
Visual Basics for Application
Visual Basics for Application Visual Basics for Application
Visual Basics for Application Raghu nath
Ā 
Chapter 4 ā€” Variables and Arithmetic Operations
Chapter 4 ā€” Variables and Arithmetic OperationsChapter 4 ā€” Variables and Arithmetic Operations
Chapter 4 ā€” Variables and Arithmetic Operationsfrancopw
Ā 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#Prasanna Kumar SM
Ā 
Introduction to visual basic 6 (1)
Introduction to visual basic 6 (1)Introduction to visual basic 6 (1)
Introduction to visual basic 6 (1)Mark Vincent Cantero
Ā 
Chapter 04
Chapter 04Chapter 04
Chapter 04llmeade
Ā 
Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4Mahmoud Ouf
Ā 
Core C# Programming Constructs, Part 1
Core C# Programming Constructs, Part 1Core C# Programming Constructs, Part 1
Core C# Programming Constructs, Part 1Vahid Farahmandian
Ā 
Chapter 08
Chapter 08Chapter 08
Chapter 08llmeade
Ā 

What's hot (15)

Book management system
Book management systemBook management system
Book management system
Ā 
Chapter 05
Chapter 05Chapter 05
Chapter 05
Ā 
Literals,variables,datatype in C#
Literals,variables,datatype in C#Literals,variables,datatype in C#
Literals,variables,datatype in C#
Ā 
Chapter 3 ā€” Program Design and Coding
Chapter 3 ā€” Program Design and Coding Chapter 3 ā€” Program Design and Coding
Chapter 3 ā€” Program Design and Coding
Ā 
Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4Fundamentals of Programming Chapter 4
Fundamentals of Programming Chapter 4
Ā 
Visual Basics for Application
Visual Basics for Application Visual Basics for Application
Visual Basics for Application
Ā 
Chapter 4 ā€” Variables and Arithmetic Operations
Chapter 4 ā€” Variables and Arithmetic OperationsChapter 4 ā€” Variables and Arithmetic Operations
Chapter 4 ā€” Variables and Arithmetic Operations
Ā 
Operators and Expressions in C#
Operators and Expressions in C#Operators and Expressions in C#
Operators and Expressions in C#
Ā 
Introduction to visual basic 6 (1)
Introduction to visual basic 6 (1)Introduction to visual basic 6 (1)
Introduction to visual basic 6 (1)
Ā 
Chapter 04
Chapter 04Chapter 04
Chapter 04
Ā 
Intake 38 4
Intake 38 4Intake 38 4
Intake 38 4
Ā 
Csharp
CsharpCsharp
Csharp
Ā 
Visual Logic Project - 1
Visual Logic Project - 1Visual Logic Project - 1
Visual Logic Project - 1
Ā 
Core C# Programming Constructs, Part 1
Core C# Programming Constructs, Part 1Core C# Programming Constructs, Part 1
Core C# Programming Constructs, Part 1
Ā 
Chapter 08
Chapter 08Chapter 08
Chapter 08
Ā 

Viewers also liked

TechnikwĆ¼rze - Vorstellung und HintergrĆ¼nde zum Podcast fĆ¼r Webentwickler
TechnikwĆ¼rze - Vorstellung und HintergrĆ¼nde zum Podcast fĆ¼r WebentwicklerTechnikwĆ¼rze - Vorstellung und HintergrĆ¼nde zum Podcast fĆ¼r Webentwickler
TechnikwĆ¼rze - Vorstellung und HintergrĆ¼nde zum Podcast fĆ¼r WebentwicklerDavid Maciejewski
Ā 
Š¾ŃŠ½Š¾Š²Š½Ń‹Šµ Š½Š°ŠæрŠ°Š²Š»ŠµŠ½Šøя_Š¼ŠµŃ‚Š¾Š“._рŠ°Š±Š¾Ń‚Ń‹_2015-2016_ŠµŃ‚стсŠ¾_
 Š¾ŃŠ½Š¾Š²Š½Ń‹Šµ Š½Š°ŠæрŠ°Š²Š»ŠµŠ½Šøя_Š¼ŠµŃ‚Š¾Š“._рŠ°Š±Š¾Ń‚Ń‹_2015-2016_ŠµŃ‚стсŠ¾_ Š¾ŃŠ½Š¾Š²Š½Ń‹Šµ Š½Š°ŠæрŠ°Š²Š»ŠµŠ½Šøя_Š¼ŠµŃ‚Š¾Š“._рŠ°Š±Š¾Ń‚Ń‹_2015-2016_ŠµŃ‚стсŠ¾_
Š¾ŃŠ½Š¾Š²Š½Ń‹Šµ Š½Š°ŠæрŠ°Š²Š»ŠµŠ½Šøя_Š¼ŠµŃ‚Š¾Š“._рŠ°Š±Š¾Ń‚Ń‹_2015-2016_ŠµŃ‚стсŠ¾_vikaevp
Ā 
Transparent Object Proxies for JavaScript
Transparent Object Proxies for JavaScriptTransparent Object Proxies for JavaScript
Transparent Object Proxies for JavaScriptMatthias Keil
Ā 
Dna sequencing
Dna sequencingDna sequencing
Dna sequencingNeeraj Sharma
Ā 
MentalHealthParity
MentalHealthParityMentalHealthParity
MentalHealthParityTim Rozelle
Ā 
Desarrollo sostenible
Desarrollo sostenibleDesarrollo sostenible
Desarrollo sostenibleMariaezquivel
Ā 
Viber katarzyna kołodziej
Viber katarzyna kołodziejViber katarzyna kołodziej
Viber katarzyna kołodziejkasiczek23
Ā 
Research methods
Research methodsResearch methods
Research methodsArfan rai
Ā 
Icelandic-Fish-and-Seafood-Catalogue_English (1)
Icelandic-Fish-and-Seafood-Catalogue_English (1)Icelandic-Fish-and-Seafood-Catalogue_English (1)
Icelandic-Fish-and-Seafood-Catalogue_English (1)Thuc To
Ā 
key concepts of marketing 1
key concepts of marketing 1key concepts of marketing 1
key concepts of marketing 1Neeraj Sharma
Ā 
Programming inexcelvba anintroduction
Programming inexcelvba anintroductionProgramming inexcelvba anintroduction
Programming inexcelvba anintroductionOfun Emma
Ā 
Baisse de concentration suite Ć  interruption
Baisse de concentration suite Ć  interruptionBaisse de concentration suite Ć  interruption
Baisse de concentration suite Ć  interruptionaproposdelles
Ā 
E learning excel vba programming lesson 1
E learning excel vba programming  lesson 1E learning excel vba programming  lesson 1
E learning excel vba programming lesson 1Vijay Perepa
Ā 
Intro macros in Excel 2007
Intro macros in Excel 2007Intro macros in Excel 2007
Intro macros in Excel 2007Hasrudin Tazep
Ā 

Viewers also liked (20)

Vba
Vba Vba
Vba
Ā 
Excel chapter-7
Excel chapter-7Excel chapter-7
Excel chapter-7
Ā 
Excel y visual basic
Excel y visual basicExcel y visual basic
Excel y visual basic
Ā 
TechnikwĆ¼rze - Vorstellung und HintergrĆ¼nde zum Podcast fĆ¼r Webentwickler
TechnikwĆ¼rze - Vorstellung und HintergrĆ¼nde zum Podcast fĆ¼r WebentwicklerTechnikwĆ¼rze - Vorstellung und HintergrĆ¼nde zum Podcast fĆ¼r Webentwickler
TechnikwĆ¼rze - Vorstellung und HintergrĆ¼nde zum Podcast fĆ¼r Webentwickler
Ā 
Š¾ŃŠ½Š¾Š²Š½Ń‹Šµ Š½Š°ŠæрŠ°Š²Š»ŠµŠ½Šøя_Š¼ŠµŃ‚Š¾Š“._рŠ°Š±Š¾Ń‚Ń‹_2015-2016_ŠµŃ‚стсŠ¾_
 Š¾ŃŠ½Š¾Š²Š½Ń‹Šµ Š½Š°ŠæрŠ°Š²Š»ŠµŠ½Šøя_Š¼ŠµŃ‚Š¾Š“._рŠ°Š±Š¾Ń‚Ń‹_2015-2016_ŠµŃ‚стсŠ¾_ Š¾ŃŠ½Š¾Š²Š½Ń‹Šµ Š½Š°ŠæрŠ°Š²Š»ŠµŠ½Šøя_Š¼ŠµŃ‚Š¾Š“._рŠ°Š±Š¾Ń‚Ń‹_2015-2016_ŠµŃ‚стсŠ¾_
Š¾ŃŠ½Š¾Š²Š½Ń‹Šµ Š½Š°ŠæрŠ°Š²Š»ŠµŠ½Šøя_Š¼ŠµŃ‚Š¾Š“._рŠ°Š±Š¾Ń‚Ń‹_2015-2016_ŠµŃ‚стсŠ¾_
Ā 
Ɓfrica
Ɓfrica Ɓfrica
Ɓfrica
Ā 
Transparent Object Proxies for JavaScript
Transparent Object Proxies for JavaScriptTransparent Object Proxies for JavaScript
Transparent Object Proxies for JavaScript
Ā 
Dna sequencing
Dna sequencingDna sequencing
Dna sequencing
Ā 
MentalHealthParity
MentalHealthParityMentalHealthParity
MentalHealthParity
Ā 
Desarrollo sostenible
Desarrollo sostenibleDesarrollo sostenible
Desarrollo sostenible
Ā 
Viber katarzyna kołodziej
Viber katarzyna kołodziejViber katarzyna kołodziej
Viber katarzyna kołodziej
Ā 
Spelling
SpellingSpelling
Spelling
Ā 
Research methods
Research methodsResearch methods
Research methods
Ā 
Europa
EuropaEuropa
Europa
Ā 
Icelandic-Fish-and-Seafood-Catalogue_English (1)
Icelandic-Fish-and-Seafood-Catalogue_English (1)Icelandic-Fish-and-Seafood-Catalogue_English (1)
Icelandic-Fish-and-Seafood-Catalogue_English (1)
Ā 
key concepts of marketing 1
key concepts of marketing 1key concepts of marketing 1
key concepts of marketing 1
Ā 
Programming inexcelvba anintroduction
Programming inexcelvba anintroductionProgramming inexcelvba anintroduction
Programming inexcelvba anintroduction
Ā 
Baisse de concentration suite Ć  interruption
Baisse de concentration suite Ć  interruptionBaisse de concentration suite Ć  interruption
Baisse de concentration suite Ć  interruption
Ā 
E learning excel vba programming lesson 1
E learning excel vba programming  lesson 1E learning excel vba programming  lesson 1
E learning excel vba programming lesson 1
Ā 
Intro macros in Excel 2007
Intro macros in Excel 2007Intro macros in Excel 2007
Intro macros in Excel 2007
Ā 

Similar to Notes how to work with variables, constants and do calculations

Similar to Notes how to work with variables, constants and do calculations (20)

PRELIM-Lesson-2.pdf
PRELIM-Lesson-2.pdfPRELIM-Lesson-2.pdf
PRELIM-Lesson-2.pdf
Ā 
Introduction to Visual Basic
Introduction to Visual Basic Introduction to Visual Basic
Introduction to Visual Basic
Ā 
Contact management system
Contact management systemContact management system
Contact management system
Ā 
Introduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdfIntroduction to Programming Fundamentals 3.pdf
Introduction to Programming Fundamentals 3.pdf
Ā 
Program logic and design
Program logic and designProgram logic and design
Program logic and design
Ā 
Understanding Code Formats in Vista
Understanding Code Formats in VistaUnderstanding Code Formats in Vista
Understanding Code Formats in Vista
Ā 
Programming concepts By ZAK
Programming concepts By ZAKProgramming concepts By ZAK
Programming concepts By ZAK
Ā 
Chapter 2- Prog101.ppt
Chapter 2- Prog101.pptChapter 2- Prog101.ppt
Chapter 2- Prog101.ppt
Ā 
C programming session 09
C programming session 09C programming session 09
C programming session 09
Ā 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
Ā 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
Ā 
c#.pptx
c#.pptxc#.pptx
c#.pptx
Ā 
Visual Basic Review - ICA
Visual Basic Review - ICAVisual Basic Review - ICA
Visual Basic Review - ICA
Ā 
ADBMS ASSIGNMENT
ADBMS ASSIGNMENTADBMS ASSIGNMENT
ADBMS ASSIGNMENT
Ā 
Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++Introduction to Procedural Programming in C++
Introduction to Procedural Programming in C++
Ā 
COM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptxCOM 211 PRESENTATION.pptx
COM 211 PRESENTATION.pptx
Ā 
Vb6.0 intro
Vb6.0 introVb6.0 intro
Vb6.0 intro
Ā 
Ms vb
Ms vbMs vb
Ms vb
Ā 
Introduction to ā€˜Cā€™ Language
Introduction to ā€˜Cā€™ LanguageIntroduction to ā€˜Cā€™ Language
Introduction to ā€˜Cā€™ Language
Ā 
Naveen Kumar Bokku
Naveen Kumar BokkuNaveen Kumar Bokku
Naveen Kumar Bokku
Ā 

Recently uploaded

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
Ā 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park masabamasaba
Ā 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...masabamasaba
Ā 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyviewmasabamasaba
Ā 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
Ā 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
Ā 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
Ā 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
Ā 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
Ā 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfkalichargn70th171
Ā 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
Ā 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfWilly Marroquin (WillyDevNET)
Ā 
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļøcall girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļøDelhi Call girls
Ā 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...masabamasaba
Ā 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
Ā 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...panagenda
Ā 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is insideshinachiaurasa2
Ā 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
Ā 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
Ā 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastPapp KrisztiƔn
Ā 

Recently uploaded (20)

VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
Ā 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
Ā 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
Ā 
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
%in Hazyview+277-882-255-28 abortion pills for sale in Hazyview
Ā 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
Ā 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
Ā 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Ā 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Ā 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
Ā 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Ā 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
Ā 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
Ā 
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļøcall girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
call girls in Vaishali (Ghaziabad) šŸ” >ą¼’8448380779 šŸ” genuine Escort Service šŸ”āœ”ļøāœ”ļø
Ā 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
Ā 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
Ā 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
Ā 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
Ā 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
Ā 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
Ā 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
Ā 

Notes how to work with variables, constants and do calculations

  • 1. Development Software 2 ā€“ DOS200S Compiled By WH. Olivier Ā©2013 Cape Peninsula University of Technology Window Form Applications Using Visual C++ Variables, Constants and Calculations Objectives and Learning Areas: For this lesson you will learn how to do the follow: 1. Knowing the different data types 2. How to define constant and variable identifiers 3. The scope of identifiers and where to declare identifiers 4. How to convert data from one type to another. Introduction For any program that you will be developing in Visual C++, or any other computer language for that matter, has three fundamental components to it. It is: INPUT: what will be the input values that will be fed into the program by the user? PROCESS: what are the operations to be performed by the program? OUTPUT: what are the results that will be produced by the program as feedback to the user? When designing an application program, the requirements for the application would state what the Inputs are, what process must be performed on the input data, and what would be the output produced. The components explained above are shown with the use of an application. Storing and retrieving data Data in the application above is stored in the Text property of the textBox object (control) on screen. For every application that you develop, there must be a way to store and retrieve data. There are various ways in which to store data and this is dependent on how you would want to use the data in the application. Here some way: 1. Constants. These are data types that are declared with a set value and cannot be changed throughout the running of the application. A good example would be the number of months or number of days in a week. const int WEEKDAYS = 7; INPUTS PROCESS OUTPUT INPUTS
  • 2. Development Software 2 ā€“ DOS200S Compiled By WH. Olivier Ā©2013 Cape Peninsula University of Technology 2. Variables. These identifiers are there to store data that are expected to change throughout the program. When the user inputs different values, a calculation will produce different results for each input. Variables in this case are best suited. An example would be a different price for a different item record. double itemPrice; 3. Arrays. When you want to store a list or collection of related information, you can declare and array and store the collection in it. Each individual item can them be reference via an index and using the same identifier name. An example would be if you want to store a list of names or test scores. int testScores[5]; 4. Structures. This identifier is a user-defined data type which comprises of other data types and is designed to suite the programmer needs. With this identifier, related variables are grouped into a single variable where by each individual data member will be referenced using a access operator. An example would be related student information like student number, name, subjects and scores. struct StudDetail { int studNum; string name; string subjects[5]; }; Data Types and Usage The table below shows the various data types that can be used in Visual C++. Also indicated are the managed and unmanaged uses. C++ Data Type Common Language Runtime (CLR) Data Types (managed) Primitive data types (unmanaged) Use For Storage Size in Bytes Sample data Boolean Boolean bool True or False values 2 flag Byte Byte - 0 ā€“ 255, binary data 1 singleNumber Char - char Single Unicode character 2 Gender (M/F) Date DateTime 1/1/0001 ā€“ 12/31/9999 8 currentDate Decimal Decimal float Decimal fractions 16 Rand Double Double double Double-precision floating- point numbers 8 PI Single Single Single-precision floating- point numbers 4 Length Short Int16 short Small integers 2 Age Integer Int32 int Whole numbers 4 NumGoals Long Int64 long Large whole numbers 8 IDnumber String String^ string Alphanumeric data (characters) Varies Name Object Object (class / struct) Any type of data 4
  • 3. Development Software 2 ā€“ DOS200S Compiled By WH. Olivier Ā©2013 Cape Peninsula University of Technology Data declarations and scope of identifiers Depending on where you declare an identifier, it can either be local or global. This means that if a variable is declared within a function or procedure, it is local to that procedure or function. If, however, you would want that identifier to be global, you would declare it outside of any function block. Letā€™s look at the following example to illustrate local and global identifiers. A windows form application calculates the gross salary by adding the basic salary to the bonus amounts. The calculation is done on the press of a button. Local Variable Declarations private: System::Void btnCalc_Click(System::Object^ sender, System::EventArgs^ e) { double basic = 0; double bonus = 0; basic = Double::Parse(txtBSalary->Text); bonus = Double::Parse(txtBonus->Text); double salary = basic + bonus; txtGSalary->Text = "R " + salary.ToString("F"); } Local variables can only be used within the function or procedure where it is declared in. This relates to the scope of the identifier. Global Variable Declarations Global identifiers are not declared in any function or procedure. Their scope is global and therefore can be used in any other function within the current form. There are two places in the code editor where global variables can be declared. The first area is in the ā€˜Required designer variableā€™ section. As shown below, it is just before the area where code for all the controls on screen are generated. /// <summary> /// Required designer variable. /// </summary> Int32 intClickCounter; Int32 intTimerCounter; DateTime myTime; #pragma region Windows Form Designer generated code As the comment indicates, you can only declare variables in this area. Here you can use any data type which suites the requirements. The second area is just before the class definition of the current form as shown here below. /// <summary> /// Summary for Form1 /// </summary> int globalNumber = 0; public ref class Form1 : public System::Windows::Forms::Form {... Global variable declared in the ā€˜Require designer variableā€™ section cannot be initialized there. The best place to initialize these variables is when the form is loaded as shown below. private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { intClickCounter = 0; intTimerCounter = 30; } Local Variable declarations. The variables are initialized to 0 Conversion from String to double. The values on in the Text property of the textbox control is converted to double and assigned to the double variables Global Variable declarations. These variables are declared in the ā€˜Required designer variableā€™ section. Global Variable declarations. Declared just before the Form1 class definition. It is also initialized here. Global Variables Initialized. Variables are initialized to 0 and 30 respectably.
  • 4. Development Software 2 ā€“ DOS200S Compiled By WH. Olivier Ā©2013 Cape Peninsula University of Technology Constant declarations Constants can either be global or local. When they are declared locally, they are declared with in a procedure or function. There are two ways in which to define constants. - Using the const reserve word. - - Using the #define preprocessor #pragma once const int MONTHS = 12; #define LENGTH 10 #define WIDTH 5 namespace SalaryCalc1 {... Take note of where constants are being declared above. These are declared as global constants. Converting values from one data type to another. When working with values where by the user must enter data in a textbox and that data must be used in some calculation, these values must be converted to an equivalent type which would allow the calculation to be done. Consider the following screen layout of Salary Calculation Application; In this example the user is requested to enter the Basic Salary and Bonus amounts. On pressing the ā€˜Calculate Salaryā€™ button, the application will take the values in the textboxes as entered by the user and use that in a calculation to produce the ā€˜Gross Salaryā€™. Note that values entered in on screen are all regarded as Strings and can therefore not be used within a calculation. For this reason, these values must first be converted to an appropriate format which would allow the calculation to be done. Here is the code that would accomplish this. private: System::Void btnCalc_Click(System::Object^ sender, System::EventArgs^ e) { double basic = Double::Parse(txtBSalary->Text); double bonus = Double::Parse(txtBonus->Text); double salary = basic + bonus; txtGSalary->Text = "R " + salary.ToString("F"); } Constant named MOUNT defined using const reserve word Constants named LENGTH and WIDTH defined using #define preprocessor. Converting from String to double Converting from double to String