C Programming h
C Programming h
A program is a set of
instructions for a computer to
follow. The input to a
computer can be thought of
as consisting of two parts, a
program and some data. The
computer follows the
instruction in the program,
and in that way, performs
some process.
• An expression terminated by a
semicolon is referred to as a statement.
Void main()
{
}
The program shown doesn’t actually do anything
because it contains no C++ statements. To create a
program that does something, you must place one or
more C++ statements between the opening and
closing braces.
Tips: Placing the main() header and the pair of
braces on three separate lines is a matter
of style. The program void main() { }
works as well as one written on three lines.
As a matter of style, however, most C++
programmers give void main() a line of its
own. They then give each brace a line of
its own, and indent each bracket a few
spaces.
void main()
{
int myAge;
int yourAge;
char myMiddleInitial;
double myMoney, yourMoney;
}
• Notice the integer variables myAge and
yourAge are each declared in a
separate statement. On the other hand,
the two doubles, myMoney and
yourMoney, are declare in the same
statement. When you declare two
variables within the same statement,
you separate the variable names with a
comma and place the semicolon at the
end of the list of all the variables of that
type.
• Stating the value of a variable is called
assignment, and is achieved with the
assignment operator =. You can
assign a value to a variable in the same
statement that declares the variable, or
assign the value later in another
statement. Assigning a value to a
variable upon creation is often referred
to as initializing the variable.
Declaring, initializing, and assigning values to
variables:
int midtermScore;
int finalScore = 100,
midtermScore = 76;
int quiz1Score = 10,
quiz2Score = 5;
#include<iostream.h>
void main()
{
cout<<”Hi there”;
}
• To indicate a newline character, you can use
the escape sequence \n. The backslash
removes the usual meaning of the letter n,
(which is simply to produce the letter n, as in
the statement cout<<’n’) and causes the
output to move to a new line. The statement
cout<<”Hi\nthere”; shows “Hi” and “there” in
two separate lines.
int main ()
{
double price;
cout << "Please enter the price";
cin >>price;
cout << "The price you entered is " <<price<<endl;
getch();
}
C++ Classes and Objects:
int book_count = 0;
book_count = books_on_shelf + books_on_order;
cout <<“the value of book_count:” <<book_count;
Note:
The first statement is a declaration statement.
book_count is called an identifier, a variable or an object.
It defines an area of computer memory associated
with the name book_count that holds integer
values.
0 is a literal constant.
book_count is initialized to a first value of zero.
The second statement is an assignment statement.
It places in the area of computer memory
associated with book_count the result of adding
together books_on_shelf and books_on_order.
Jade A. Buenavides
BSCPE-5
College of Engineering
Sultan Kudarat State University
Isulan Campus
jadebuenavides@sksu.edu.ph
0917-000-0001
Activity: Discuss the following:
a. Data Structure
b. Object Oriented Programming
c. Abstract Data Type
d. Inheritance
--end--