C++ Graphichs Session1
C++ Graphichs Session1
Instructor Inputs
2.2 Instructor Inputs NIIT
Session Overview
This session covers Chapter 2.
Slide 1
Creating Objects
Objectives
In this session, you will learn to:
Identify variables and data types
Write and execute a C++ program
Use arrays
Start the session by sharing the session objectives with the students.
Creating Objects
Variables
Are Named locations in memory that contain specific
values
Rules for naming variables in C++:
Should not have any embedded spaces or symbols
Should be unique
Can have any number of characters
Must begin with a letter or an underscore, which
may be followed by a sequence of letters, digits, or
underscores
Keywords cannot be used as variable names
Explain variables to the students. Tell them that programs deal with data. This data needs
to be stored for further manipulation in the program. To store the data temporarily we use
variables.
Explain the rules for naming variables in C++. Write some variable names and ask the
students whether they are valid or not.
Creating Objects
Data Types
Define the type of data that can be stored in a variable
Built-in data types are:
char - For characters and strings
int - For integers
float - For numbers with decimals
Use this and the next slide to explain the built-in data types to the students. Ask the
students that when the char data type can store number, characters, and symbols in a
character data type then what is the need for the int data type.
After getting answers from the students, explain the difference between the char and int
data type. Also, give them a brief idea when to use which data type.
Creating Objects
Number of
Bytes on a Minimum Maximum
Data Type
32-bit Value Value
Computer
Slide 5
Creating Objects
Member Variables
Are used to implement attributes in C++
Are declared inside the class body
Example:
class Car
{
float price;
};
Use this slide to explain the significance of member variables. Tell the students that
member variables are bound with a class. Give them a few examples on how to declare
member variables. Ask the students to declare member variables of a particular class.
Creating Objects
Use this slide to explain how to accept and store values in a member variable.
Slide 7
Creating Objects
Use this slide to explain the components of a program. Explain the significance of each
component.
Creating Objects
Use this and the next to indicate the steps involved in Compiling, Linking, and Executing
a Program.
Slide 9
Creating Objects
Creating Objects
Array
Is a collection of elements of a single data type stored
in adjacent memory locations
Syntax:
<data_type> <variable_name>[<dimension_size>];
Example:
int arr[5];
Explain the use and importance of arrays. Tell the students that how difficult it would be
to create a program for storing and displaying the age of 100 students without using an
array. Also, explain the benefits of an array in such type of programs.
Discuss the syntax for declaring an array. Explain the example given in this and the next
slide.
Creating Objects
Slide 12
Creating Objects
Creating Objects
String Constant
Is an array of characters terminated by a
NULL(‘\0’)
Example:
char str[] = "SANDY";
Can be schematically represented as:
Explain the use of a character array. When explaining the NULL terminator in an array,
stress that NULL is a single character represented in C++ programs as \0. Students make
the common mistake of assuming that the NULL character must be present at the end of
arrays of all data types. Stress that this is true only for strings.
The character NULL is a string terminator and should not be referred to as NULL. The
macro NULL is used to initialize and compare pointers and may or may not be defined as
zero by different implementations of C++ compilers. The character with the American
Standard Code for Information Interchange (ASCII) value of zero is called the NULL
character. This character is represented in a C++ program as \0. The macro NULL can be
used in a C++ program but the word NULL cannot. The NULL character cannot be
displayed on the screen.
Creating Objects
Use slide 14 and 15 to conduct the activity in the class. Ask the students to provide a
solution to the problem statement.
After the students complete the solution, you can provide them with the solution of the
problem. You can find the solution in the TIRM CD at the following path.
Creating Objects
Slide 16
Creating Objects
Use slide 14 and 15 to conduct the activity in the class. Ask the students to provide a
solution to the problem statement.
Slide 17
Creating Objects
Slide 18
Creating Objects
Summary
In this lesson, you learned that:
A variable is a named location in memory that
contains a specific value
A data type defines the type of data that can be stored
in a variable
Member variables are declared inside the class body
The cin object is used to accept input from the
keyboard
The contents of header files are inserted into a
program with the #include directive
The C++ program execution starts from the first
statement of the main()function
©NIIT OOPS/Lesson 2/Slide 18 of 20
Use this and the next two slides to summarize the session.
Creating Objects
Summary (Contd.)
Comment entries are notes that a programmer writes
anywhere in the code so that any programmer reading
the code will understand it better
An object is an instance of a class
The compiler is a software that translates a program
written in a language like C++ into machine language
and the file containing the translated program is called
the object code of your program
Linking combines your object code with the object
code of the functions you use and adds some
standard startup code to produce a run-time version of
your program
©NIIT OOPS/Lesson 2/Slide 19 of 20
Slide 20
Creating Objects
Summary (Contd.)
The Dos-based DJGPP compiler for C++ generates
the executable code and stores it in a file named
a.exe
An array is a collection of elements of a single data
type stored in adjacent memory locations
The array can be initialized when it is defined or later
An array must be given a constant dimension size,
which should be at least 1
Each element of an array can be accessed by its
subscript number
Ans. The compiler resolves the name arr into the address of the array. The statement
arr[3] is resolved by the compiler to imply *(arr + 3).
Ans. Any number. Depends on the amount of memory the computer has.
Ans. Yes. But even public members of the class are not accessible outside the container
class.