0% found this document useful (0 votes)
58 views16 pages

C++ Graphichs Session1

Graphics Session for C++

Uploaded by

ganeshk85730
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views16 pages

C++ Graphichs Session1

Graphics Session for C++

Uploaded by

ganeshk85730
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 16

Session 2

Instructor Inputs
2.2 Instructor Inputs NIIT
Session Overview
This session covers Chapter 2.

Prerequisite Knowledge for the Session


To understand the concepts covered in the session, the students should be aware of the
following concepts/skills:
 Object-Oriented methodology
 Object-Oriented analysis and design
 Creating classes

Tips for Handling the Session


This session introduces programming to the students. You need to explain the
fundamental concepts in detail to enable the students to create programs with ease. Use
the slides Programming_Using C++_Session_02.pps to conduct the session.

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

©NIIT OOPS/Lesson 2/Slide 1 of 20

Start the session by sharing the session objectives with the students.

NIIT Instructor Inputs 2.3


Slide 2

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

©NIIT OOPS/Lesson 2/Slide 2 of 20

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.

2.4 Instructor Inputs NIIT


Slide 3

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

©NIIT OOPS/Lesson 2/Slide 3 of 20

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.

NIIT Instructor Inputs 2.5


Slide 4

Creating Objects

Data Types (Contd.)

Number of
Bytes on a Minimum Maximum
Data Type
32-bit Value Value
Computer

char 1 -128 127

int 4 -2^31 (2^31)-1

float 4 -10^39 10^39

©NIIT OOPS/Lesson 2/Slide 4 of 20

Slide 5

Creating Objects

Member Variables
 Are used to implement attributes in C++
 Are declared inside the class body
 Example:
class Car
{
float price;
};

©NIIT OOPS/Lesson 2/Slide 5 of 20

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.

2.6 Instructor Inputs NIIT


Slide 6

Creating Objects

Accepting and Storing Values in Member


Variables
 The cin object is used to accept input from the user
Example:
class Car
{
float price;
public:
void acceptprice()
{
cout << “Enter Price :”;cin >> price;
}
};

©NIIT OOPS/Lesson 2/Slide 6 of 20

Use this slide to explain how to accept and store values in a member variable.

Slide 7

Creating Objects

Writing and Executing a C++ Program


 The iostream header file
 Is called a pre-processor directive
 The main() function
 Is mandatory in C++ programming for program
execution
 Creating objects
 Is required for reserving memory at the time of
declaration

©NIIT OOPS/Lesson 2/Slide 7 of 20

Use this slide to explain the components of a program. Explain the significance of each
component.

NIIT Instructor Inputs 2.7


Slide 8

Creating Objects

Compiling, Linking, and Executing a Program


 Is done by following the listed steps:
1. The C++ program should contain the #include
statement, the class declaration and member
function definition, and the main() function.
2. Save the file with a .cc extension.
3. Compile the file using the gpp <file name>
command from the command prompt.
4. Execute the file using the a.out command from
the command prompt.

©NIIT OOPS/Lesson 2/Slide 8 of 20

Use this and the next to indicate the steps involved in Compiling, Linking, and Executing
a Program.

Slide 9

Creating Objects

Executing C++ Programs

a.out executes the initial


startup code

The startup code executes


the main() function

When the main() function


finishes execution, it sends a
status of execution to the
operating system

©NIIT OOPS/Lesson 2/Slide 9 of 20

2.8 Instructor Inputs NIIT


Slide 10

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];

©NIIT OOPS/Lesson 2/Slide 10 of 20

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.

NIIT Instructor Inputs 2.9


Slide 11

Creating Objects

Declaring and Initializing an Array


 Example:
arr[0] = 14;
arr[1] = 15;
arr[2] = 17;
arr[3] = 45;
arr[2] = 81;
 Example:
int arr[5] = {14, 15, 17, 45, 81};
 Example:
int arr[] = {14, 15, 17, 45, 81};

©NIIT OOPS/Lesson 2/Slide 11 of 20

Slide 12

Creating Objects

Declaring and Initializing an Array (Contd.)


 Size of an array should be specified at the time of its
declaration
Example:
char err[]; //ERROR!! will not compile
 An array cannot be initialized with another array
Example:
xyz = abc; // ERROR!!

©NIIT OOPS/Lesson 2/Slide 12 of 20

2.10 Instructor Inputs NIIT


Slide 13

Creating Objects

String Constant
 Is an array of characters terminated by a
NULL(‘\0’)
Example:
char str[] = "SANDY";
Can be schematically represented as:

©NIIT OOPS/Lesson 2/Slide 13 of 20

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.

NIIT Instructor Inputs 2.11


Slide 14

Creating Objects

Problem Statement 2.D.1


As a member of a team that is developing the billing
system software for Diaz Telecommunications Inc., you
have been assigned the task of creating a software
module that accepts the following customer details and
displays it.
1. Mobile number, containing a maximum of 12
characters
2. Name, containing a maximum of 25 characters
3. Date of birth, containing a maximum of 10 characters
4. Billing address, containing a maximum of 50
characters

©NIIT OOPS/Lesson 2/Slide 14 of 20

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.

DATAFILES FOR FACULTY\Programming Using C++\2.D.1\2d1.cc

2.12 Instructor Inputs NIIT


Slide 15

Creating Objects

Problem Statement 2.D.1 (Contd.)


5. City, containing a maximum of 25 characters
6. Residence phone number, containing a maximum of
13 characters.
7. Amount outstanding, containing decimal values

©NIIT OOPS/Lesson 2/Slide 15 of 20

Slide 16

Creating Objects

Problem Statement 2.P.1


As a part of the team that is developing the billing system
software for Diaz Telecommunications Inc., you have
been assigned the task of writing a program that accepts
dealer details. The details to be captured are given
below:
 First Name
 Last Name
 City
 Phone number

©NIIT OOPS/Lesson 2/Slide 16 of 20

Use slide 14 and 15 to conduct the activity in the class. Ask the students to provide a
solution to the problem statement.

NIIT Instructor Inputs 2.13


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.

DATAFILES FOR FACULTY\Programming Using C++\2.P.1\2P1.cc

Slide 17

Creating Objects

Problem Statement 2.P.1 (Contd.)


Write a C++ program to accept and display the dealer
details.
The dealer details should be displayed in the following
format:
First name: Last name:

City: Phone number:

©NIIT OOPS/Lesson 2/Slide 17 of 20

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.

2.14 Instructor Inputs NIIT


Slide 19

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

©NIIT OOPS/Lesson 2/Slide 20 of 20

NIIT Instructor Inputs 2.15


FAQs
The following questions can be asked by the student in the session:

1. How is the statement, cout << arr[3];, resolved by the compiler?

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).

2. How many objects can a class have?

Ans. Any number. Depends on the amount of memory the computer has.

3. Can there be two classes with the same name?

Ans. Yes, provided the classes exist in a separate scope or namespace.

4. Can there be a class within a class?

Ans. Yes. But even public members of the class are not accessible outside the container
class.

5. Can there be a function within a function?

Ans. No, a function cannot be defined within another function.

2.16 Instructor Inputs NIIT

You might also like