Académique Documents
Professionnel Documents
Culture Documents
Queries: harishk@pu.ac.in
Session I
Queries: harishk@pu.ac.in 2
Computer Fundamentals
Queries: harishk@pu.ac.in 3
Software
• Computer software is …
– The collection of programs used by a computer
• Includes:
– Various System Software like Editors, Translators, Operating Systems etc.
– Various Application Software like Word Processor, Simulation Software etc.
Queries: harishk@pu.ac.in 4
Queries: harishk@pu.ac.in 5
Computer Organization
• Main memory -------- Memory locations containing the running program (RAM)
• Secondary memory -- Permanent record of data often on a disk (e.g. Tape, Hard
Disk, Floppy Disk etc. )
Queries: harishk@pu.ac.in 6
Computer Memory
• Main Memory
Queries: harishk@pu.ac.in 7
Larger Data Items
Queries: harishk@pu.ac.in 8
int sum(int[] x) {
int sum = 0; 00101010101010
n = 0; 10101011111010
while (n < x.length) {
sum += x[n];
11101010101110
} 00101010101010
return sum; ...
}
Queries: harishk@pu.ac.in 9
Data or Code?
• ‘A’ may look like 01000001
Queries: harishk@pu.ac.in 10
Secondary Memory
• Secondary memory
– Stores instructions and data between sessions
Queries: harishk@pu.ac.in 11
Computer Input
• Computer input consists of
– A program
– Some data
Queries: harishk@pu.ac.in 12
High-level Languages
Queries: harishk@pu.ac.in 13
Compilers
– Source code
• the original program in a high level
language
– Object code
• the translated version in machine
language
Queries: harishk@pu.ac.in 14
Incomplete list of C++ compliers
• Apple C++
• Bloodshed Dev – C++
• Borland C++
• Cygwin (GNU C++)
• Intel C++ for Linux
• GNU CC Source
• Borland C++
• HP C++
• IBM C++
• SUN C++
Queries: harishk@pu.ac.in 15
Linkers
• Some programs we use are already compiled
– Their object code is available for us to use
– For example: Input and output routines
• A Linker combines
– The object code for the programs we write
and
– The object code for the pre-compiled routines
into
The machine language program the CPU can run
Queries: harishk@pu.ac.in 16
Programming and Problem Solving
• Algorithm
– A sequence of precise instructions which leads to a solution
• Program
– An algorithm expressed in a language the computer can understand
– A Computer program is a set of instructions that directs a computer to
perform tasks. Computer programmers use program development tools that
generate these instructions automatically or use a programming language to
write these instructions themselves.
Queries: harishk@pu.ac.in 17
Program Design
Queries: harishk@pu.ac.in 18
Problem Solving Phase
Queries: harishk@pu.ac.in 19
Implementation Phase
Queries: harishk@pu.ac.in 20
Queries: harishk@pu.ac.in 21
Session II
Queries: harishk@pu.ac.in 22
Object Oriented Programming
• Abbreviated OOP
Queries: harishk@pu.ac.in 23
OOP Characteristics
• Encapsulation
– Information hiding
– Objects contain their own data and algorithms
• Inheritance
• Polymorphism
Queries: harishk@pu.ac.in 24
Introduction to C++
Queries: harishk@pu.ac.in 25
A Sample C++ Program
• A simple C++ program begins this way
#include <iostream.h>
int main()
{
Queries: harishk@pu.ac.in 26
Queries: harishk@pu.ac.in 27
Program Layout (1/3)
– Place opening brace ‘{‘ and closing brace ‘}’ on a line by themselves
– Indent statements
– Use only one statement per line
Queries: harishk@pu.ac.in 33
Program Layout (2/3)
• Include Directives
#include <iostream.h>
– Tells compiler where to find information about items used in the program
– iostream is a library containing definitions of cin and cout
Queries: harishk@pu.ac.in 34
Program Layout (3/3)
Queries: harishk@pu.ac.in 35
Program Errors
• Syntax errors
– Violation of the grammar rules of the language
– Discovered by the compiler
• Error messages may not always show correct location of errors
• Run-time errors
– Error conditions detected by the computer at run-time
• Logic errors
– Errors in the program’s algorithm
– Most difficult to diagnose
– Computer does not recognize an error
Queries: harishk@pu.ac.in 37
Program Style - Comments
Queries: harishk@pu.ac.in 39
C++ Basics
Queries: harishk@pu.ac.in 40
Overview
Variables and Assignments
Program Style
Queries: harishk@pu.ac.in 41
Variables and Assignments
Queries: harishk@pu.ac.in 42
Identifiers & Keywords
Queries: harishk@pu.ac.in 43
Declaring Variables
• Declaration syntax:
– Type_name Variable_1 , Variable_2, . . . ;
Queries: harishk@pu.ac.in 44
Assignment Statement
• An assignment statement changes the value of a variable
– Assignment statements end with a semi-colon
– The single variable to be changed is always on the left of the
assignment operator ‘=‘
– On the right of the assignment operator can be
• Constants -- age = 21;
• Variables -- my_cost = your_cost;
• Expressions -- circumference = diameter * 3.14159;
Queries: harishk@pu.ac.in 46
Input and Output
Queries: harishk@pu.ac.in 47
Output using cout
• cout is an output stream sending data to the monitor
• The insertion operator "<<" inserts data into cout
• Example:
cout << number_of_bars << " candy bars\n";
– This line sends two items to the monitor
• The value of number_of_bars
• The quoted string of characters " candy bars\n"
– The ‘\n’ causes a new line to be started following the ‘s’ in bars
• A new insertion operator is used for each item of output
Queries: harishk@pu.ac.in 48
Input Using cin
• cin is an input stream bringing data from the keyboard
• The extraction operator (>>) removes data to be used
• Example:
cin >> number_of_bars;
cin >> one_weight;
• This code prompts the user to enter data then reads two
data items from cin
– The first value read is stored in number_of_bars
– The second value read is stored in one_weight
Queries: harishk@pu.ac.in 52
Reading Data From cin
• Example:
cin >> v1 >> v2 >> v3;
Queries: harishk@pu.ac.in 53
Designing Input and Output
Queries: harishk@pu.ac.in 54
Data Types and Expressions
Queries: harishk@pu.ac.in 55
Writing Double Constants
Queries: harishk@pu.ac.in 56
Session III
Queries: harishk@pu.ac.in 62
Type Casting
Queries: harishk@pu.ac.in 63
Operators
Queries: harishk@pu.ac.in 66
Operators
Queries: harishk@pu.ac.in 67
Results of Operators
Queries: harishk@pu.ac.in 68
Integer Remainders
Queries: harishk@pu.ac.in 71
Control Statements
Queries: harishk@pu.ac.in 74
Queries: harishk@pu.ac.in 78
Boolean Expressions
Queries: harishk@pu.ac.in 79
&& and ||
&&
• Syntax: (Comparison_1) && (Comparison_2)
• Example: if ( (2 < x) && (x < 7) )
||
• Syntax: (Comparison_1) || (Comparison_2)
• Example: if ( (x= =1) || (x= =y) )
Queries: harishk@pu.ac.in 81
NOT
– !(x = = y)
• True if x is NOT equal to y
Queries: harishk@pu.ac.in 82
Queries: harishk@pu.ac.in 88
Queries: harishk@pu.ac.in 90
For loop
Queries: harishk@pu.ac.in 91
Break & Continue Statements
Queries: harishk@pu.ac.in 92
Array
Queries: harishk@pu.ac.in 94
Session IV
Queries: harishk@pu.ac.in 95
Functions
Queries: harishk@pu.ac.in 96
Function Call Syntax
• Function_name (Argument_List)
– Argument_List is a comma separated list:
• Example:
– side = sqrt(area);
– cout << “2.5 to the power 3.0 is “ << pow(2.5, 3.0);
Queries: harishk@pu.ac.in 97
Queries: harishk@pu.ac.in 98
Programmer-Defined Functions
– Function definition
• Describes how the function does its task
;
• Can appear before or after the function is called
• Syntax:
Type_returned Function_Name(Parameter_List)
{
//code to make the function work
}
Queries: harishk@pu.ac.in 99
Function Declaration
• Tells the return type
• Tells the name of the function
• Tells how many arguments are needed
• Tells the types of the arguments
• Tells the formal parameter names (optional)
– Formal parameters are like placeholders for the actual
arguments used when the function is called
• Example:
double total_cost(int number_par, double price_par);
• Example:
return (subtotal + subtotal * TAX_RATE);
• Examples:
double total_cost(int number_par, double price_par);
• Compiler checks that the types of the arguments are correct and in the
correct sequence.
• Compiler cannot check that arguments are in the correct logical order
• Pass by value
• Pass by address
• Pass by reference
age 1001 34
initial 1002 A
hours 1003 23.5
1004
• Example:
void good_stuff(int& par1, int par2, double& par3);
– par1 and par3 are call-by-reference formal parameters
• Changes in par1 and par3 change the argument variable
– par2 is a call-by-value formal parameter
• Changes in par2 do not change the argument variable
– Because the main function is defined to return a value of type int, the return
is needed
– C++ standard says the return 0 can be omitted, but many compilers still
require it
• Also possible:
void abc ( int = input());
• C++ allows more than one definition for the same function name
– Very convenient for situations in which the “same” function is needed for
different numbers or types of arguments
• Polymorphism???
• Overloaded functions
AND / OR
– A programmer needs to know what will be produced if the proper arguments are put
into the box
• Also allows us
– To change or improve a function definition without forcing programmers
using the function to change what they have done
– To know how to use a function simply by reading the function declaration
and its comment
Queries: harishk@pu.ac.in
What is Object Oriented Programming?
• Class Provider
– made or purchased
– need for documentation of the class -- API
• Class User
– programmer or development team
• Program User or End User
• Need to use the scope resolution operator to identify which class the
function is associated with
• Need to specify return type and formal parameters the same as given in
declaration
• Within the function you can refer to the data members of the receiving
object using the data member names
class Change
{
private:
int rupees, dollars, pounds;
…..
public:
Change (int initvalue)
{ rupees=initvalue; dollars=initvalue; pounds = initvalue; cout <<“Object
created.”; } …..
};
This constructor can be used as follows:
– Change firstObject(10);
• The class Change2 can now access the private attributes rupees etc..:
class Change2
{
void convert(Change& c )
{ c.rupees = c.dollar * 45; }
};
• A::B::C xyz
• xyz is object of innermost class C.
base_object.setX(7);
derived_object.setX(12);
derived_object.setY(1);
}
Queries: harishk@pu.ac.in 157
C++ Syntax: private inheritance
• Derived class declares a constructor that takes the same char pointer parameter
Class AccountCustomer : public Customer
{
AccountCustomer(char * name_in);
};
• A derived class always inherits the destructor of the base class. The
derived class destructor is called first. This is the reverse of the
sequence for constructors
• The C++ syntax uses “function names” prefixed with “operator” for
overloading operators.
#include <vector>
– Vector names are placed in the standard namespace so the usual using
directive is needed: