unit 1 oodp basic cpp full-1
unit 1 oodp basic cpp full-1
• For example
• cout<<“\n Enter the Marks”;
• cin>> ComputerNetworks>>OODP;
Formatted Input and Output Operations
Formatted I/O
I/O class
Manipulat
function and
ors
flages
#include<iostream.h>
#define PI 3.14159
main()
{
cout.precision(3);
cout.width(10);
cout.fill(‘*’);
cout<<PI;
Output
*****3.142
Formatting Output Using Manipulators
• C++ has a header file iomanip.h that
contains certain manipulators to format the
output
Data Types in C++
Variables
A variable is the content of a memory location that stores a certain value. A variable is identified or denoted by a variable name.
The variable name is a sequence of one or more letters, digits or underscore, for example: character_
Rules for defining variable name:
A variable name can have one or more letters or digits or underscore for example character_.
White space, punctuation symbols or other characters are not permitted to denote variable name.
A variable name must begin with a letter.
Variable names cannot be keywords or any reserved words of the C++ programming language.
Data C++ is a case-sensitive language. Variable names written in capital letters differ from variable names with the same
name but written in small letters.
LocalLocal
variable:
Variables These Instance variable:
Instance Variables Static Static Variables
variables: Constant Variables
Constant is
are the variables These are the variables Static variables are
something that
which are declared which are declared in also called as class
doesn't change. In C
within the method of a a class but outside a variables. These
language and C++
class. method, constructor or variables have only
we use the keyword
any block. one copy that is shared
const to make
Example: Example: by all the different
program elements
public class Car { public class Car { objects in a class.
constant.
public: private: String Example:
Example:
void display(int m){ color; public class Car {
const int i = 10;
// Method // Created an instance public static int
void f(const int i)
int model=m; variable color tyres;
class Test
// Created a local Car(String c) // Created a class
{
variable model { variable void init(){
const int i;
cout<<model; color=c; tyres=4;
};
} }} }}
C++ Arrays
Arrays
Used to store a collection of elements (variables)
type array-name[size];
Meaning:
This declares a variable called <array-name> which contains <size>
elements of type <type>
The elements of an array can be accessed as: array-name[0],…array-
name[size-1]
Example:
int a[100]; //a is a list of 100 integers, a[0], a[1], …a[99]
double b[50];
char c[10];
Memory representation
Array example
//Read 100 numbers from the user
#include <iostream.h>
void main() {
int i, a[100], n;
i=0; n=100;
while (i<n) {
cout << “Input element “ << i << “: ”;
cin >> a[i];
i = i+1;
}
//do somehing with it ..
}
Example
Two dimensional array
Example
Problems
Write a C++ program to read a sequence of (non-negative) integers from the user
ending with a negative integer and write out
• Example:
– The user enters: 3, 1, 55, 89, 23, 45, -1
– Your program should compute the average of {3, 1, 55, 89, 23, 45} etc
Pointers
• Pointer is variable in C++
• It holds the address of another variable
• Syntax data_type *pointer_variable;
• Example int *p,sum;
Assignment
• To assign the address of variable to pointer-ampersand symbol (&)
• p=∑
• integer type pointer can hold the address of another int variable
• P=∑//assign address of another variable
• cout<<∑ //to print the address of variable
• cout<<p;//print the value of variable
How to use it
• Example of pointer
#include<iostream.h>
using namespace std;
int main()
{ int *p,sum=10; Output:
p=∑ Address of sum : 0X77712
cout<<“Address of sum:”<<&sum<<endl;Address of sum: 0x77712
cout<<“Address of sum:”<<p<<endl; Address of p: 0x77717
cou<<“Address of p:”<<&p<<endl; Value of sum: 10
cout<<“Value of sum”<<*p;
}
Pointers and Arrays
• assigning the address of array to pointer don’t
use ampersand sign(&)
#include <iostream>
using namespace std; OUTPUT:
int main(){ 0
//Pointer declaration 1
int *p; 2
//Array declaration 3
int arr[]={1, 2, 3, 4, 5, 6}; 4
//Assignment 5
p = arr; 6
for(int i=0; i<6;i++){
cout<<*p<<endl;
//++ moves the pointer to next int position
p++;
}
return 0;
This Pointers
• this pointer hold the adderss of current object
• int num;
• This->num=num;
Function using pointers
void swap(int *a,int *b)
{
#include<iostream>
int c;
using namespace std;
void swap(int *a ,int *b ); c=*a;
//Call By Reference *a=*b;
int main() *b=c;
{ }
int p,q;
cout<<"\nEnter Two Number You Want To Swap \n";
cin>>p>>q; Output:
swap(&p,&q); Enter Two Number You Want to
cout<<"\nAfter Swapping Numbers Are Given below\n\n"; Swap
cout<<p<<" "<<q<<" \n"; 10 20
return 0;
After Swapping Numbers Are
}
Given below
20 10
Type conversion and type
casting
• Type conversion or typecasting of variables refers to
changing a variable of one data type into another. While type
conversion is done implicitly, casting has to be done
explicitly by the programmer.
Type Casting
• Type casting an arithmetic expression tells the compiler to represent the value
of the expression in a certain format.
• It is done when the value of a higher data type has to be converted into the
value of a lower data type.
• However, this cast is under the programmer’s control and not under the
compiler’s control. The general syntax for type casting is
destination_variable_name=destination_data_ty--pe(source_variable_name);
float sal=10000.00;
int income;
Income=int(sal);
Class and objects
Class:
60
Object creation
Example for objects
- Data members Can be of any type,
built-in or user-defined.
This may be,
• non-static data member
Each class object has its own copy
Syntax:
static data_type datamember_name;
Here
static is the keyword.
data_type – int , float etc…
datamember_name – user defined
69
Static Data Member
Rectangle
class Rectangle r1;
{ Rectangle
r2;
private: Rectangle
int width; r3;
Syntax:
static data_type datamember_name;
Here
static is the keyword.
data_type – int , float etc…
datamember_name – user defined
72
Static Data Member
Rectangle
class Rectangle r1;
{ Rectangle
r2;
private: Rectangle
int width; r3;
static int x = 1;
x = ++x;
int y = 1;
y = ++y;
cout<<"x = "<<x<<"n";
cout<<"y = "<<y<<"n";
}
int main()
{
Test();
Test();
return 0;
}
Static Member Functions in C++
public: example1.show_n();
example2.show_n();
void set_n(){
Example::show_Number();
n = ++Number;
} return 0;
}
void show_n(){
cout<<"value of n = "<<n<<endl;
}
};