0% found this document useful (0 votes)
72 views50 pages

Compiler:: Collector

The document provides an introduction to basic C++ concepts like variables, data types, input/output functions, conditional statements, and looping structures. It includes code examples demonstrating how to write, compile, and run a simple "Hello World" program in C++. Various C++ programming constructs like if/else statements, switch/case statements, and functions like cout and cin are explained through examples.

Uploaded by

Arman Asgharpoor
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)
72 views50 pages

Compiler:: Collector

The document provides an introduction to basic C++ concepts like variables, data types, input/output functions, conditional statements, and looping structures. It includes code examples demonstrating how to write, compile, and run a simple "Hello World" program in C++. Various C++ programming constructs like if/else statements, switch/case statements, and functions like cout and cin are explained through examples.

Uploaded by

Arman Asgharpoor
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/ 50

COLLECTOR : seyed sajjad ale mohammad

COMPILER :reza sepas yar


www.itinahwazpnu.blogfa.com

C++

C++ •

! " •

)% &( '&#$% •

* % •

1 2 / 0 ." -, + •

- 56 3 - 4 & 3- " 3-/2 3-/

- (, 2 •
C++

<9 : 6 # ; C++ 4 * 7 - 8 •

<9=6 >?@ A B* ; :&=C#- *%; 8 9 * Brace •

< 2 , D = /2 ; 8 9 * 4 6 >?@ * •

< 2 , D F& %:2 * /* … */ EE •

:&=C#- 9=2 * ,92 #B6 9 % # 9= G- * % .* H 3 - &I - / =2 •

<92 * 6 6 LM #6 9 * - 4 J K 2+ 2 97 6 6 ; * 9= % #

< 2 1 N D% O P* ;Q 6 R S :&* . Case Sensitive 4 * ; C •

if, char, while,… 1T <9 2 , D ;Q 6 R S * 9 * # S ,92 #B6 •

/U0 S ) * VG W X? =V &I V% •

char a ; JT &I + &I W ; &I V% W 0 •

float pi=3.14; JT .* H + .* H W Z 9Y ; .* H V% W 0 •
! ! !

"
! ! !

# $ %

% & ' ( )* + ,

- ' % ' ( )* + ,

"# $ %& !

/\& JT )B#7 [B#7

. X .

( * '&GY% *

! ) #! )

] D%

/ ,9 #&N * /

3 3& 0 3 & 12- 0


3 3 4 3 OR 4
3& 3 & 5 3&& XOR 5

3 & 6+ 3& , ; 1#) 6

3 & 3& 77 . /* .D&2 77

3& 3 & 88 ^Q /* .D&2 88


' $ %& !

/\& JT )B#7 [B#7

a = a × -1 -a /= N

a=a+1 a++ 9S ; _ P0 ))

a=a-1 a-- 9S ; _- 6

$ (" ) $ %& !

/\& JT )B#7 [B#7

& 7 ( P* 7

: 9 979 9 )Q 6 8

: 7; G ( P* 7;

& ( 8; G )Q 6 8;

& 919 ;;9#9 G% ;;

: <; G <;

)*+ $ %& !

/\& JT )B#7 [B#7

& + 7 ,00+ <; , Y = 12- 00

: +9 97 ,44+ 8 ( , Y = => 44

& <+ 7 , `&Y <

," $ %& !

/\& JT )B#7 [B#7

; XG ;
; . .; XG X .;

; * *; XG '&GY% *;

; ) ); XG #! );

; ; XG ] D% ;

a&Fb 9Y value (

x ccc* * a 9ccc2 *
K 2XG ;+$ ,?3
y * * $= &d

< 2

- . $ /

75K J * cin 92 * 9&B6 /FDb PC /6 9 75K .0 *

<92 * ! cout 92 * [@ # /6 9 ! " /*

cout - .

cout << "Hello World";

<9=6 f Q _ # /FDb * This is a test 7e 0

cout << x;

<9 0 _ # /FDb /* x &I F e 0

J T 4 =7 /* 3 6 /* 7; *; _&* . :)# , * insertion [B#7 . 57


cout << "Hello" << "World!";

<9- - &I -/2 &6 % 4 ) insertion [B#7

int age=25;
cout << "My age is:" << age;
My age is: 25

J T 4 =7 /* <'&=6 , D endl n . 57 9 9! g" - /* J Y *

cout << "First Line.\n";


cout << "Second Line.\n Third Line.";
First Line.
Second Line.
Third Line.
cout << "First Line." << endl;
cout << "Second Line." << endl;
First Line.
Second Line.

cin $

< 6 /* . 57 , #- cin

int age;
cin >> age;

[ 9) J V J T 4 =7 /* < 6 - 9Y &I ; _&* 4 % cin ; g %

9=2 *
cin >> a >> b;

Visual C++ 6.0 Hello World $ ( $ -

X ? j 0 * <9&=6 X ? File\New\Win32 Console Application / * ! hi

,k i &G W

<9&=6 X ? An Empty Project 1b S 6

,92 X ? Add to Project /6 9 2 :l# , 6 K File\New\C++ Source File &G

<.

9&=6 96 cpp 9 Gi * 1 0 <m

#include <iostream.h>

int main()
{
cout << "Hello World!"<< endl;
return 0;
}

* 9- " b /* /\& 39&=6 ;&B6 Ctrl+F5 h F7 9 *

rld!
Press any key to continue<9=6 X GS , ; .S G g&F / *

#include <iostream.h>
int main()
{
int r;
const float pi=3.1459;

cout <<"Enter Radius: ";


cin >>r;
cout <<"If radius="<<r<<" Circumference is:
"<<2*pi*r<<endl;
cout <<"If radius="<<r<<" Area is: "<<pi*r*r<<endl;

return 0;
}
' $ 01 2

goto

W &i . 8 Db o % Mo 97 / * J T 4 =7 /* < 2 W\ GQ * ; /* n 2 4 9* i

<9- _ # End!

#include <iostream.h>

int main()
{

int n=11;

loop1: n--;
cout<<n<<endl;
if(n!=0)
goto loop1;

cout << "End!"<<endl;


return 0;

if 3 else .

if( )
{
;
}
else
{
;
}

< # RpS brace 4 % 9=2 * "; /6 % b

<. & " else A5* , D

9=6 X GS W /! / V - /@ / * J T 4 =7 /*

#include <iostream.h>
#include <math.h>

int main()
{
float a,b,c,delta;

cout <<"Enter a, b and c: "<<endl;


cin >>a>>b>>c;
delta=b*b-4*a*c;
if(delta>=0)
{
cout <<"X1="<<(-b+sqrt(delta))/(2*a)<<endl;
cout <<"X2="<<(-b-sqrt(delta))/(2*a)<<endl;
}
else
cout<<"Roots are not real!"<<endl;

return 0;
}

Switch - Case .

switch( )
{
case :
;
break;
case :
;
break;
.
.
default:
n ;
}

9=6 / F /\& ,92 X ? *% * /0 ( * 6 97 ; / * J T 4 =7 /*

#include <iostream.h>
#include <math.h>

int main()
{
float num;
int operation;

cout <<"Enter a number"<<endl;


cin >>num;
cout<<"Enter 1, 2, 3 or 4 to seleting sin(), cos(),
tan() or Log()"<<endl;
cin>>operation;

switch(operation)
{
case 1:
cout<<"sin of "<<num<<" is "<<sin(num)<<endl;
break;
case 2:
cout<<"cos of "<<num<<" is "<<cos(num)<<endl;
break;
case 3:
cout<<"tan of "<<num<<" is "<<tan(num)<<endl;
break;
case 4:
cout<<"log of "<<num<<" is "<<log(num)<<endl;
break;
default:
cout<<"Enter 1, 2, 3 or 4!"<<endl;
}

return 0;
}

While .

while( )
{
;
}

<9=6 e 6 6. " * 6 3q " * / *

#include <iostream.h>
int main()
{

char c=0;

while(c!='e')
{
cout<<"Enter e to exit"<<endl;
cin>>c;
}

return 0;

Do/While .

1N 9S 3/YBS 1" : * =* 3 2 _ /YBS 8 n 2 While R5" * . S :

<9 2 ! )

do{
;
} while( )
For $ ()&#

for( ; ; )
{
;
}

<9=6 X GS 4 1 60 /0 ( * 6 97 ; / *

#include <iostream.h>

int main()
{

int i,n;
double fact=1;

cout<<"Enter a Number: ";


cin>>n;

for(i=1;i<=n;i++)
fact*=i;
cout<<"Factorial of "<<n<<" is "<<fact<<endl;

return 0;
}
4

92 * b /* * % V%

! ", + *%W *% 8 ( )

{
BF - &I

*%

<9=6 f Q 4 V) , 6 .0 97 ; /6 9&G =* / * JT

#include <iostream.h>

long int cube(int x){


return x*x*x;
}

void main(void){
int a;
cout<<"Enter a Number: ";
cin>>a;
cout<<"Cube of "<<a<<" is: "<<cube(a)<<endl;
}

<9 ( V% 9 * '- 9! 9=2 * # V% 1* N [ 9) 1" * %

<9=6 f Q ,92 .0 97 '# P6 /6 9&G =* / * JT

#include<iostream.h>

int _max(int a,int b){


if(a>b)
return a;
else
return b;
}

void main(void){
int a,b;
cout<<" Enter Tow Numbers: "<<endl;
cin>>a>>b;
cout<<"Maximum of "<<a<<" & "<<b<<" is
"<<_max(a,b)<<endl;
}

/ * 9* 9 * *% / =2 /6 D% : * 3 # 0 V P& +, *% 9V* *% 4 %

.2 4 % P& b /* 1N / * J T 4 =7 /* < 2 V%

#include<iostream.h>

int _max(int a,int b);

void main(void){
int a,b;
cout<<" Enter Tow Numbers: "<<endl;
cin>>a>>b;
cout<<"Maximum of "<<a<<" & "<<b<<" is
"<<_max(a,b)<<endl;
}

int _max(int a,int b){


if(a>b)
return a;
else
return b;
}

;#6 / * W \G /* 4 % Header 1 0 ; 4 457 P\ / ?* 6 ; * % V% *

< #

5 6 $ #

; * /6 / * #GN /* C <9= ( &I 4 , S ,92 V% 4 &I /6 / * #GN

1* N ,92 V% A5* 4 #- &I ; <9= ( A5* ; . ,92 $F # .D!

<92 * # , D 1* N A5* q " , *

< 6 '&- " .0 % W I&i / * J T 4 =7 /*

#include <iostream.h>

void main( )
{
{
int x= 1;
}
x=x+1;
}
4 =7 /* <92 * , D 1* N ,92 / " =2 3 * % W #% * 2 V% * % /#- q " /6 &I

<92 * &V :&=Q 3 &I / * JT

#include <iostream.h>

int x=5;
int test();

void main(){
cout<<x<<endl;
cout<<test()<<endl;
}

int test(){
return x;
}

9&B6 /#B6 C 39=6 rDS P& 9V* - " 0 * " 9Y 3 * % BF &I ; '&- ?* (

<9- N &" static

static int x=s;

< * 9- " Mo 97 , #- ! " '&=6 ! static 4 9* / * ( J T 4 =7 /*

#include <iostream.h>
int test();

void main()
{
cout<<test()<<endl;
cout<<test()<<endl;
}

int test(){
static int x=10;
x = x*2;
return x;
}

4 7 89:$; 0<

4 ( 3 * % " 0 /6 [=- < # V% j 0 _&i 4 =7 /* -4 ( * % *4 %

J T 4 =7 /* (92 9- " J * % /* 34 ( 4 j 0 _&i 9Y 392 * ,92 RpS j 0 _&i 9Y

#include <iostream.h>
double volume(double r=1, double h=1);
double pi=3.145;

void main(){

cout<<"The default volume is: "<<volume()<<endl;


cout<<"The volume with r=10: "<<volume(10)<<endl;

cout<<"The volume with r=10 h=5 is:


"<<volume(10,5)<<endl;

double volume(double r, double h){


return pi*r*r*h;
}

Scope Resolution Operator # = '> %& !

/* /! % * < # V% * % ; , 9F #7 &I * W =#- BF &I 4 % 3'& /6 #-

/Q #7 W =#- &I /* * *%; , 9F /6 . : 2 t /6 /BlG :

/ * <. , N &" /= ;&)D% [ [B#7 C U= : /* u ! )-

<9- 4@ [B#7 * 6, F

#include <iostream.h>

int amount = 123; // global variable

void main()
{
int amount = 456; // local variable
cout << ::amount << endl; // Print the global
variable (123)
cout << amount << endl; // Print the local
variable (456)
}

4 ? @ Overload

/ D( =$ 1#7 : /* /6 # V% 4 G) W * * % 9=Q /6 ! 4 ) : C++

<. - 4 /* -4 ( , + P # [ 9) * % /C < 2

#include <iostream.h>

int abs(char i);


int abs(int d);
int abs(long l);

int main(){

cout << abs(-10) << endl;


cout << abs(-11) << endl;
cout << abs(-12) << endl;
return 0;
}

int abs(char i){


return i<0 ? -i : i;
}

int abs(int d){


return d<0 ? -d : d;
}

int abs(long l){


return l<0 ? -l : l;
}

- 4 . 8

,92 J 9Y /6 ! 4) : ' 6, D * % /* : ( J * 4 =6 % /6 2 R5" *

&I 4 2J * % /* &I 6 ; /) ! /* [ 7 * <9=6 &&I% 4 g % * % /*

J T 4 =7 /* < &( N *% , D &I " ,92 J

#include <iostream.h>
void inc(int &x,int &y);

void main()
{
int a=1 ,b=2;
cout<<"a = "<<a<<" and b = "<<b<<endl;
inc(a,b);
cout<<"a = "<<a<<" and b = "<<b<<endl;
}

void inc(int &x , int &y)


{
x++;
y++;
}

@( * 9Y ; _&* 9 % *%1N R5" * 2 /US5 / *: /6 #-

<92 * / 2
(8 . $

( < BA

( < ?C!

, + / W vJ K];

/* 4 B 63 @ /2 / J K /6 % b < # - 9Y b /* 4 % / b =7

< &( U ,92 , >&$?% b =7 9V%

int a[5] = {1, 2 , 3, 4, 5}

<9=6 ' # -4 * /0 ( * 6 97 Mo /6 / * JT

#include <iostream.h>

void main()
{
int num[10];
int i,j;

for(i=0;i<10;i++){
cout<<"Enter Number "<<i+1<<": ";
cin>>num[i];
}
cout<<endl<<"Index"<<" Number"<<endl;

for(i=0; i<10;i++){
cout<<i+1<<"\t "<<num[i]<<"\t";
for(j=0; j<num[i]; j++)
cout<<'|';
cout<<endl;
}
}

(D

<9 2 , &i - 6 6 / * -/2 C4*

char str[]="Test";

392 * . @ 2 6 6 /* n * /6 9S ; , 5V* : /#B6 J K * . * *e 0/ J K

< 6 V% P& b /* 4 4 % : * =*

char str[]={'T','e','s','t',\0'}
<9 " /2 Y 4 % *

#include <iostream.h>

char str[10];

void main(){
cout<<"Enter Your Name: ";
cin >> str;
cout<<"Hi "<<str<<" !"<<endl;
}

.2 4 % / ; - / " 9= #- / 2 ; - 6 6 ;% ;% /*

#include <iostream.h>

char str[10];
int i;

void main(){
cout<<"Enter Your Name: ";
cin >> str;

for(i=0;str[i]!='\0';i++)
cout<<str[i]<<endl;
}

- 6 - " <w

B? , -+ 9= % " ; b =7 , * / 0 ." , + :& - "

<9- 4@ " ; V% , F J T <9=2 *

struct studentRec
{
char studebtName[16];
float midTermGrade;
float homeworkGrade;
float finalGrade;
float aveGrade;
};

$=7 s ,92 \ " 9=6 \ studentRec W * " ; struct /#B6 e 0

<9=2 * @7 /&Y* / 2 + -4 ) /6 92 *

: '& % " \ hi : * =* . 9 9! , + ; V% N " ; 457

<'&=6 V% + : - &I '&=6 , D 9 9! , +

studentRec firstStudent;
studentRec Student[5];
. $ E! (

; $=7 /* * & * / Y [B#7 <'&=6 , D < / Y [B#7 " ; b =7 /* * & *

J T 4 =7 /* 3 &( N, D 3 &I ; W ] K "

firstStudent.finalGrade = 17;

<9- 4@ - 6 ; - 9Y V% , F / * JT

#include <iostream.h>

struct studentRec
{
char studebtName[16];
float midTermGrade;
float homeworkGrade;
float finalGrade;
float aveGrade;
};

studentRec firstStudent;

void main(){

cout<<"Enter Student Name: ";


cin>>firstStudent.studebtName;

cout<<endl<<"Enter Mid Term Grade: ";


cin>>firstStudent.midTermGrade;

cout<<endl<<"Enter Homework Grade: ";


cin>>firstStudent.homeworkGrade;

cout<<endl<<"Enter Final Grade: ";


cin>>firstStudent.finalGrade;

firstStudent.aveGrade = (firstStudent.midTermGrade
+firstStudent.homeworkGrade+firstStudent.finalGrade)/3;

cout<<endl<<"The Average Grade of


"<<firstStudent.studebtName<<" is
"<<firstStudent.aveGrade<<endl;

# i 6 9V% U " + / V% * 4 %

#include <iostream.h>

struct studentRec
{
char studebtName[16];
float midTermGrade;
float homeworkGrade;
float finalGrade;
float aveGrade;
x

studentRec Student[5];

void main(){
int i;
for(i=0;i<5;i++){

cout<<"Enter Student Name "<<i+1<<": ";


cin>>Student[i].studebtName;

cout<<endl<<"Enter Mid Term Grade: ";


cin>>Student[i].midTermGrade;

cout<<endl<<"Enter Homework Grade: ";


cin>>Student[i].homeworkGrade;

cout<<endl<<"Enter Final Grade: ";


cin>>Student[i].finalGrade;
cout<<"-------------------------"<<endl;
}

for(i=0;i<5;i++)
Student[i].aveGrade = (Student[i].midTermGrade
+

Student[i].homeworkGrade+Student[i].finalGrade)/3;

cout<<"Name\t"<<"Grade"<<endl;

for(i=0;i<5;i++){

cout<<Student[i].studebtName<<"\t"<<Student[i].aveG
rade<<endl;
}

cout<<"-------------------------"<<endl;
}

- Union <L
4 /6 D% : * 3, * " /& 2 , + : <92 * Union / 0 ." , + :&

gY0 2 # /0 ( U " y7 W #% * y0 - " R5" * 3+ : / =2 ; V%

" 9= #- Union V% < 2 V% /U0 S /K * y0 &I 4 , D 4

<92 *

union studentRec
{
char studebtName[16];
float midTermGrade;
float homeworkGrade;
float finalGrade;
float aveGrade;
};

hi /6 D% : * <'&=6 , D < / Y [B#7 Union ; b =7 /* * & * " ; 9= #-

< :&* B N 9Y Union $=7 - - 9Y

studentRec.homeworkGrade = 15;
studentRec.finalGrade = 18;
studentRec.aveGrade = 17;
- 56 <m

FC@ 5 6

V% - " 9= #- 8 56 <9 ! * \ , + V% 4 ) - 56 /B& * C++

/#B6 g % 56 ; .9=2 * 56 y7 4 =7 /* 9= % P& * % 3 8 56 /6 D% : * 9 2

< 2 V% class 9&B6

class TimeType{

public:
void setTime(int,int,int);
void Increment();
void Decrement();

private:
int hour;
int minute;
int second;
}
92 * *& 1* N 56 y7 * % * 8=% 2 V% ' $ 9V* /6 V* % , y7 -

*% ! 56 * W =#- /6 y7 * % <9=2 * # 1* N / * 1" '&Y G K /*

:&&V% , y7 /& Y /6 . b $?* y7 * % 3,9 *%< 2 ,9&

3 ( " 0 6 " K /* ,9 * % 39=6 \ 56 2/ * /6 [=- <9=6

<9 ( # * 9Y , 6 /& - 9Y - i

56 + &2 4 % 9 ( V% : ' : 56 /6 [=- 3 - " 9= #-

< 6 \: ' :

TimeType GlobalTime;
TimeType TimeArray[5];

JT

#include <iostream.h>

class Cube
{
public:
Cube();
void setSide(double s);
double getSide();
double Area();
double Volume();
void Properties();
private:
double Side;
};
void main()
{
int side;

cout<<" Enter side of cube: ";


cin>>side;
cout<<endl;

Cube MyCube;

MyCube.setSide(side);
MyCube.Properties();

//----------------------------------

Cube::Cube()
{
Side=0;
}

void Cube::setSide(double s)
{
Side = s <= 0 ? 1 : s;
}

double Cube::getSide()
{
return Side;
}

double Cube::Area()
{
return 6 * Side * Side;
}

double Cube::Volume()
{
return Side * Side * Side;
}

void Cube::Properties()
{
cout << "Characteristics of this cube";
cout << "\nSide = " << getSide();
cout << "\nArea = " << Area();
cout << "\nVolume = " << Volume() <<endl<<endl;
}
, # V% [ 1 0 P\ b /* 4 '& % 56 ; 4 6 / G 6 / * W \G *

< # V% Header 1 0 56 [

.c 9 Gi * Bb 1 0

#include <iostream.h>
#include "cube.h"

void main()
{
int side;

cout<<" Enter side of cube: ";


cin>>side;
cout<<endl;

Cube MyCube;

MyCube.setSide(side);
MyCube.Properties();
}
cube.c 1 0 56 V%

#include <iostream.h>
#include "cube.h"

Cube::Cube()
{
Side=0;
}

void Cube::setSide(double s)
{
Side = s <= 0 ? 1 : s;
}

double Cube::getSide()
{
return Side;
}

double Cube::Area()
{
return 6 * Side * Side;
}
double Cube::Volume()
{
return Side * Side * Side;
}

void Cube::Properties()
{

cout << "Characteristics of this cube";


cout << "\nSide = " << getSide();
cout << "\nArea = " << Area();
cout << "\nVolume = " << Volume() <<endl<<endl;
}

cube.h 1 0 56 457

class Cube
{
public:
Cube();
void setSide(double s);
double getSide();
double Area();
double Volume();
void Properties();
private:
double Side;
};

FC@ $ E! ( FC@ #

56 /6 V* % 56 y7 * % 9 ,92 V% 56 /6 - &I 56 ; y7 -,

1 0 , S 39=2 * # 56 y7 /6 V* % 9=2 * ]BV 56 : , S /* 9 ,92 V%

4 % 9=2 * 1* N 56 y7 * % g % 56 y7 /#- 3 56 ; , S <9=2 *

[B#7 56 y7 /* * 56 , S 4 &* < 6 /V! 8 /* #&Y G 4 @ g %

39=2 * *%, S 39 ,92 V% 56 y7 * % ; /6 - &I <'&=6 , D B / Y

56 , S /6 &I W * W =#- &I 3 y7 * % ( <9 ,92 / " =2 * % 4 #- * gY0 =V

# '&Y G K /* * % 1" 92 * 56 , S /6 &I b: 392 * ,92 V% .

< N &I W 1N ' > [B#7 9 * 4 /* * 6+ ! 4 %

E! ( +@

56 y7 * % , y7 /* , F 3' $ ' =V y7 /* 80 V

p 392 * ' $ 56 y7 * % , y7 /* * j 0 _&i . S <9==6 J =6

/0 ( U b $" 39 ,9 R V GQ * :& 1N 56 W :&&V% 9V* /6 y7 /#-

/* n * x 9V* GQ * % y7 /#- /* 39=6 :&&V% /K * R V /6 S 3R V - hi <9 2


1* N 56 4 y7 * % ] K gY0 56 ; b $" y7 < 2 J #7 56 V% 4 i

<9=2 * 1* N / * V* % - g % 56 #7 y7 1* Y 9=2 *

I H ; C@ $ G

56 ; V% , F <9= ( $ ,92 ] @ 56 Bb 56

92 * b /* ,92 *z

class derived-class-name : access base-class-name


{
// body of class
};

J T 4 =7 /*

#include <iostream.h>

class BaseClass {
int i;
public:
void set(int n);
int get();
};
class DerivedClass : public BaseClass {
int j;
public:
void setJ(int n);
int mul();
};

void BaseClass::set(int n)
{
i = n;
}

int BaseClass::get()
{
return i;
}

void DerivedClass::setJ(int n)
{
j = n;
}

int DerivedClass::mul()
{
return j * get();
}

int main()
{
DerivedClass ob;
ob.set(10); // load i in BaseClass
ob.setJ(4); // load j in DerivedClass

cout << ob.mul()<<endl; // displays 40

return 0;
}
A - (, 2

0 D 5 6

; /* '&Y G K /* &I ; W <9=6 98[ " /U0 S -/ " 3 (, 2 , +

J T 4 =7 /* <9=6 /V! 9Y ; /* '&Y G &d K /* ( , 2 ; 9=6 /V! 3 9Y

(9=6 V% + (, 2 ' &I + &I

int a , *p ;

0 [B#7

J T 4 =7 /* <9 ( * " 9 B#7 /U0 S / " [B#7 :

#include<iostream.h>

void main()
{
int a=10;
int *p;
p=&a;
cout<<p<<endl;
cout<<*p<<endl;
}

0 D J 4 ( ? 0<

* * % 1" ,92 J &I 9=Q ; &&I% * 9= % +! 8 ( 9= - (, 2

J T 4 =7 /* <9 &( N, D * % /* O P* -, J

#include<iostream.h>

void swap(int *x, int *y);

int main(void)
{
int i, j;
i = 10;
j = 20;

swap(&i, &j); /* pass the addresses of i and j */

cout<<"i is: "<<i<<endl;


cout<<"j is: "<<j<<endl;
return 0;
}

void swap(int *x, int *y)


{
int temp;
temp = *x; /* save the value at address x */
*x = *y; /* put y into x */
*y = temp; /* put x into y */
}

- (, 2 , D 4 9* +! *4 ( J 4 ) C++ 92 * * % _?* /6 #- J T

/* : /G Y * <92 * pi 4 ) /* + ! *4 ( J : * =* <9-

9&=6 /! % / *

#include <iostream.h>

void neg(int &i); // i now a reference


int main()
{
int x;
x = 10;
cout << x << " negated is ";
neg(x); // no longer need the & operator
cout << x << "\n";
return 0;
}

void neg(int &i)


{
i = -i; // i is now a reference, don't need *
}

#include <iostream.h>

void neg(int *i);

int main()
{
int x;
x = 10;
cout << x << " negated is ";
neg(&x);
cout << x << "\n";
return 0;
}

void neg(int *i)


{
*i = -*i;
}

You might also like