0% found this document useful (0 votes)
14 views12 pages

Oop 2

This document covers the basics of C++ programming with a focus on object-oriented programming concepts such as objects and classes. It explains how to structure programs using objects, the significance of classes as blueprints for objects, and introduces fundamental programming elements like directives, functions, and input/output operations. Additionally, it provides examples of simple C++ programs and common programming constructs like loops and conditionals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Oop 2

This document covers the basics of C++ programming with a focus on object-oriented programming concepts such as objects and classes. It explains how to structure programs using objects, the significance of classes as blueprints for objects, and introduces fundamental programming elements like directives, functions, and input/output operations. Additionally, it provides examples of simple C++ programs and common programming constructs like loops and conditionals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Basics of C++ programming

Course: MPHYCC-05 Modeling and Simulation

(M.Sc. Sem-II)

Unit-1 of CC-05
Objects and Classes in OOP
Let’s briefly examine a few of the major elements of object-oriented
languages in general, and C++ in particular.

Objects
When you approach a programming problem in an object-oriented
language, you no longer ask how the problem will be divided into
functions, but how it will be divided into objects. Thinking in terms of
objects, rather than functions, has a surprisingly helpful effect on how
easily programs can be designed. This results from the close match
between objects in the programming sense and objects in the real world.

What kinds of things become objects in object-oriented programs? Here


are some typical categories:

Physical objects
Automobiles in a traffic-flow simulation
Electrical components in a circuit-design program
Countries in an economics model
Aircraft in an air traffic control system

Elements of the computer-user environment


Windows
Menus
Graphics objects (lines, rectangles, circles)
The mouse, keyboard, disk drives, printer

Data-storage constructs
Customized arrays
Stacks
Linked lists
Binary trees

Human entities
Employees
Students
Customers
Salespeople
Collections of data
An inventory
A personnel file
A dictionary
A table of the latitudes and longitudes of world cities

Classes:

In OOP we say that objects are members of classes. What does this mean?
Let’s look at an analogy. Almost all computer languages have built-in data
types. For instance, a data type int, meaning integer, is predefined in C++.
You can declare as many variables of type int as you need in your
program:
int day ;
int count ;
int divisor ;
int answer;

In a similar way, you can define many objects of the same class, as shown
in Figure. A class serves as a plan, or blueprint. It specifies what data and
what functions will be included in objects of that class. Defining the class
doesn’t create any objects, just as the mere existence of data type int
doesn’t create any variables.

A class is thus a description of a number of similar objects. This fits our


non-technical understanding of the word class. Prince, Sting, and Madonna
are members of the rock musician class. There is no one person called
“rock musician,” but specific people with specific names are members of
this class if they possess certain characteristics. An object is often called an
“instance” of a class.
Basics of C++

Before creating a class in C++, we will first discuss a little bit


about the fundamentals of C++ programming.

Simple program:

#include <iostream>

using namespace std;

int main()
{
cout << “hello world!” << end1;
return 0;
}

This program will print “hello world!” on the console (window at which
output is printed).

Directives
The two lines that begin the program are directives. The first is a
preprocessor directive, and the second is a using directive.

Preprocessor Directives
The preprocessor directive #include tells the compiler to insert another file
into your source file. In effect, the #include directive is replaced by the
contents of the file indicated. Using an #include directive to insert another
file into your source file is similar to pasting a block of text into a
document with your word processor.

#include is only one of many preprocessor directives, all of which can be


identified by the initial # sign. The use of preprocessor directives is not as
common in C++ as it is in C, but we’ll look at a few additional examples
as we go along. The type file usually included by #include is called a
header file.

IOSTREAM is an example of a header file (sometimes called an include


file). It’s concerned with basic input/output operations, and contains
declarations that are needed by the cout identifier and the << operator.
Without these declarations, the compiler won’t recognize cout and will
think << is being used incorrectly. There are many such include files. The
newer Standard C++ header files don’t have a file extension, but some
older header files, left over from the days of the C language, have the
extension . H.

The using Directive

A C++ program can be divided into different namespaces. A namespace is


a part of the program in which certain names are recognized; outside of the
namespace they’re unknown. The directive

using namespace std;

says that all the program statements that follow are within the std
namespace. Various program components such as cout are declared within
this namespace. If we didn’t use the using directive, we would need to add
the std name to many program elements. For example, in the program
we’d need to say

std::cout << “hello world!”<< end1;

To avoid adding std:: dozens of times in programs we use the using


directive instead.

Function
Functions are one of the fundamental building blocks of C++. The
program consists almost entirely of a single function called main() . The
only parts of this program that are notpart of the function are the first two
lines—the ones that start with #include and using .
We noted in last lecture that a function can be part of a class, in which case
it is called a member function. However, functions can also exist
independently of classes. We are not yet ready to talk about classes, so we
will show functions that are separate standalone entities, as main() is here.

When you run a C++ program, the first statement executed will be at the
beginning of a function called main(). The program may consist of many
functions, classes, and other program elements, but on startup, control
always goes to main() . If there is no function called main() in your
program, an error will be reported when you run the program. In most C++
programs, as we’ll see later, main() calls member functions in various
objects to carry out the program’s real work. The main() function may also
contain calls to other standalone functions.
Program Statements
The program statement is the fundamental unit of C++ programming.
There are two statements in the program:
the line

cout << “hello world!” <<end1”;

and the return statement

return 0;

The first statement tells the computer to display the quoted phrase. Most
statements tell the computer to do something. In this respect, statements in
C++ are similar to statements in other languages. In fact, as we’ve noted,
the majority of statements in C++ are identical to statements in C. A
semicolon signals the end of the statement.

the statement

cout << “hello world!” <<end1”;

causes the phrase in quotation marks to be displayed on the screen. How


does this work? A complete description of this statement requires an
understanding of objects, operator overloading, and other topics which will
be discussed later, but here’s a brief preview.

The identifier cout (pronounced “C out”) is actually an object. It is


predefined in C++ to correspond to the standard output stream. A stream is
an abstraction that refers to a flow of data. The standard output stream
normally flows to the screen display—although it can be redirected to
other output devices. The operator << is called the insertion or put to
operator. It directs the contents of the variable on its right to the object on
its left. In the program, it directs the string constant “hello world!” to cout,
which sends it to the display.
The phrase in quotation marks, “hello world!” , is an example of a string
constant. As you probably know, a constant, unlike a variable, cannot be
given a new value as the program runs. Its value is set when the program is
written, and it retains this value throughout the program’s existence.

The endl Manipulator


The last cout statement in the INTVARS program ends with an unfamiliar
word: endl . This causes a linefeed to be inserted into the stream, so that
subsequent text is displayed on the next line. It has the same effect as
sending the ‘\n’ character, but is somewhat clearer. It’s an example of a
manipulator. Manipulators are instructions to the output stream that modify
the output in various ways.
Program using Input with cin
Now that we’ve seen some variable types in use, let’s see how a program
accomplishes input. The next example program asks the user for a
temperature in degrees Fahrenheit, converts it to Celsius, and displays the
result. It uses integer variables.

The statement
cin >> ftemp;

causes the program to wait for the user to type in a number. The resulting
number is placed in the variable ftemp . The keyword cin (pronounced “C
in”) is an object, predefined in C++ to correspond to the standard input
stream. This stream represents data coming from the keyboard (unless it
has been redirected). The >> is the extraction or get from operator. It takes
the value from the stream object on its left and places it in the variable on
its right.
The next example, SQRT , uses the library function sqrt() to calculate the
square root of a number entered by the user.
The for Loop
The for loop executes a section of code a fixed number of times. It’s
usually (although not always) used when you know, before entering the
loop, how many times you want to execute the code.
Here’s an example, FORDEMO , that displays the squares of the numbers
from 0 to 14:
The if Statement

The if statement is the simplest of the decision statements. Our next


program, IFDEMO, provides an example.

You might also like