Lecture 1
Lecture 1
Programming
Programming in C++
Error Messages
First Program
• Programs in C++ are written in
blocks
• Every block has name
• The name of monitor block is main
• Every statement is ended with semi-
colon(;)
• Our first program will display a
message on output screen
Simple Program
• #include<iostream>
• #include<conio>
• int main()
•{
• cout<<"Every age has a language of its
own\n";
• return 0;
•}
Simple Program
• Now this program will pass through
two phases
1. Compile checking for errors
2. Run The output of program
Simple Program
• #include<iostream>
• #include<conio>
• int main(){
• cout<<"Every age has a language of its
own\n";
• getch();
• return 0;
•}
Comments
• #include<iostream>
• #include<conio>//Preprocessor Directive
• int main(){ //start of the function
• cout<<"Every age has a language of its
own\n";
• getch();
• return 0; //Return Control to OS
•}
• #include<iostream>
• Using namespace std;
• int main()
• {
• int base,power,result=1;
• cout<<"Plz enter base --> ";
• cin>>base;
• cout<<"Plz enter power --> ";
• cin>>power;
• for(int i=0;i<power;i++){
• result=result*base;
• cout<<"\nThe result is: "<<result;}
• return 0;
• }
Nested Loops
• We can use loops inside loop body
• Lot of care is needed in this type of
structure
• First inner loop complete its iteration
and then control shift to outer one,
this process continues till end
• #include<conio>
• #include<iostream>
• int main(){
• for(int i=0;i<5;i++){
• for(int j=0;j<5;j++){
• cout<<"*";
• }
• cout<<"\n";
• }
• getch();
• return 0;
• }
#include<conio>
#include<iostream>
int main(){
for(int i=1;i<=10;i++){
for(int j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
getch();
return 0;}
#include<conio>
#include<iostream>
int main(){
for(int i=10;i>=0;i--){
for(int j=1;j<=i;j++)
cout<<"*";
cout<<endl;
}
getch();
return 0;
}
Intro to Functions
• A function is a block of code which only runs when it is called. You can pass data,
known as parameters, into a function. Functions are used to perform certain actions,
and they are important for reusing code: Define the code once, and use it many
times.
Why functions are imp
• The program may need to repeat the same piece of code at various places or it may
require repeating same task again and again. The program may become very large
and messy if we do not divide it into sub programs (functions). The piece of code
that is executed repeatedly is stored in a separate function, and is called when and
where it is required.
Types of function
• There are two types of functions
•
1) Library Functions
2) User defined Functions
Types of function
•
1) Library Functions:
• Library functions are the built-in functions in C++. Programmers can use library
function by invoking function directly. They don’t need to write it by themselves.
•
1) User defined Functions
• C++ allows programmers to define their own functions. A user defined function
group code to perform a specific task and that group of code given a name
(identifier). When the function is invoked from any part of the program, it all
executes the code defined in the body of function.
•
Library functions
• #include<iostream>
• #include<math.h>
• using namespace std;
•
int main()
• {
• int n ;
•
cout<<" Enter your number you want square";
• cin>> n;
•
cout<<"Sqrt: " <<sqrt(n);
• cout<<"Pow: " <<pow(2, n);
• }
•
User-defined Functions
return-value-type function-name(parameter-list)
{
declarations and statements
}
• Return-Type: No(void) return or type
• Function-name: Any(identifier rules)
• Parameter: No(void) or no. of
parameters with types
Simple Function
No Return Name No Parameter
void line(){
for(int i=0;i<45;i++)
cout<<“*”;
cout<<endl;
}
Using Function
• Three important thing should be in
min when we use functions in main
function
1. Function Definition:
It is just body of function which is
below the main
2. Function declaration
3. Function call
Using Function
Function Declaration
Function Call
Function Definition
Example code Return
and with return value
• #include<iostream>
• #include<math.h>
• using namespace std;
•
// Function Prototyping // FUnction Declaration
• void sum(int a, int b);
• int sum1(int c, int d);
•
int main()
• {
• sum(10,20); // Function Calling
• cout<<"\n";
• cout<<sum1(100,200); // Function Calling
• }
•
// Function Defination
• void sum(int a, int b)
• {
• int e;
• e = a+b;
• cout<<" sum is : " << e ;
• }
• int sum1(int c, int d)
• {
• return c+d;
• }
•
• #include<iostream>
• void line();
• void hash();
• void dash();
• int main(){
• dash();
• for(int i=0;i<5;i++)
• line();
• dash();
• for(int i=0;i<5;i++)
• hash();
• getch();
• return 0;
• }
• void line(){
• for(int i=0;i<45;i++)
• cout<<"*";
• cout<<endl;
• }
• void hash(){
• for(int i=0;i<45;i++)
• cout<<"#";
• cout<<endl;
• }
• void dash(){
• for(int i=0;i<45;i++)
• cout<<"-";
• cout<<endl;
• }
Passing Values To functions
• We can pass any type of value to
function
• We have to define the data type of
the values
• During function call we send on those
values which a function can accept
Passing Values To functions
Problem
• Write a program that takes input a number in
main function and passes the number to a
function. The function will display the
factorial of that number.
solution
• #include<iostream>
• #include<math.h>
• using namespace std;
•
// Function Prototyping // FUnction Declaration
• void fac(int a);
•
int main()
• {
• int f;
• cout<<" Enter the number you want factorial: ";
• cin>>f;
• fac(f); // function calling
• }
•
// Function Defination
• void fac(int a)
• {
• int sum = 1;
• for(int i=1; i<=a; i++)
• {
• sum = sum * i;
•
• }
• cout<<" Factorial is: " << sum <<endl;
•
• }
•
Problem
Local Variable
Default Arguments
• Function call commonly pass a
particular value of an argument
• Programmer can provide default
value for that argument
• When that argument is omitted in
program the compiler use the default
value
#include<conio>
#include<iostream>
int add(int a,int b,int c=0,int d=0,int e=0){
return a+b+c+d+e;
}
int main(){
cout<<"\nCall with two arguments:"<<add(4,5);
cout<<"\nCall with three arguments:"<<add(4,5,6);
cout<<"\nCall with four arguments:"<<add(2,3,4,5);
cout<<"\nCall with five arguments:"<<add(1,3,4,3,3);
getch();
return 0;
}
Problem
function(arrayprint);
• }
•
Pass an multi dimension
array into function
• #include<iostream>
• using namespace std;
•
function(int adata[2][2], int adata2[2][2])
• {
• int adata3[2][2];
•
int sum =0;
• for(int i=0; i<2; i++)
• {
• for(int j=0; j<2; j++)
• {
• adata3[i][j] = adata[i][j] + adata2[i][j];
•
cout<< " " << adata3[i][j] << " ";
• }
• cout<<"\n";
• }
• cout<<sum;
•
• }
• int main()
• {
• int arrayprint[2][2], arrayprint2[2][2];
•
for(int i=0; i<2; i++)
• {
• for(int j=0; j<2; j++)
• {
• cout<<" Enter numebrs: ";
• cin>> arrayprint[i][j];
• }
• cout<<"\n";
• }
•
for(int i=0; i<2; i++)
• {
• for(int j=0; j<2; j++)
• {
• cout<<" Enter numebrs: ";
• cin>> arrayprint2[i][j];
• }
• cout<<"\n";
• }
•
function(arrayprint, arrayprint2);
• }
•
Function Template
• A template is a simple yet very
powerful tool in C++. The simple
idea is to pass data type as a
parameter so that we don’t need to
write the same code for different
data types
Example
• #include<iostream>
• using namespace std;
•
// Function Template.
•
template <typename T>
• T sum(T x,T y)
• {
• return x+y;
• }
•
int main()
• {
• int res = sum <int> (10,20);
• float res2 = sum <float> (10.10,20.20);
• cout<< res;
• cout<<endl;
• cout<< res2;
•
• }
Header File
• #include <iostream.h>
Re-usability
Code Maintenance
Security
Problem-solving
Difference between classes
and structures
• Technically speaking, structs and classes are almost
equivalent
• The major difference like class provides the flexibility of
combining data and methods (functions ) and it provides
the re-usability called inheritance.
• A class has all members private by default. A struct is a
class where members are public by default