basics in c++

22
1.1.2.Calculat ing with integer constants #include <iostream> using std::cout; using std::endl; int main() { cout << 1 + 2 << endl; cout << 1 - 5 << endl; cout << 1 - 2 << endl; cout << 1 * 2 << endl; cout << 1/3 << endl; cout << 1 3 << endl; cout << 1 -3 << endl; cout << -1 3 << endl; cout << -1 -3 << endl; cout << 1 + 2/1 - 5 << endl; cout << (1 + 2)/(1 - 5) << endl; cout << 1 + 2/(1 - 5) << endl; cout << (1 + 2)/1 - 5 << endl; cout << 4*5/3 + 7/3 << endl; return 0; } 3 -4 -1 2 0 1 1 -1 -1 -2 0 1 -2 4 1.1.3.Working with integer variables #include <iostream> using std::cout; using std::endl; int main() { int a = 10; int b = 3; cout << a/b;  cout << a b;

Upload: sai-sushma

Post on 10-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 1/22

1.1.2.Calculating with integer constants

#include <iostream>using std::cout;using std::endl;

int main() {cout << 1 + 2 << endl;cout << 1 - 5 << endl;cout << 1 - 2 << endl;

cout << 1 * 2 << endl;cout << 1/3 << endl;cout << 1 3 << endl;cout << 1 -3 << endl;cout << -1 3 << endl;cout << -1 -3 << endl;

cout << 1 + 2/1 - 5 << endl;

cout << (1 + 2)/(1 - 5) << endl;cout << 1 + 2/(1 - 5) << endl;cout << (1 + 2)/1 - 5 << endl;

cout << 4*5/3 + 7/3 << endl;return 0;

}

3-4-12011-1-1-201-24

1.1.3.Working with integer variables

#include <iostream>

using std::cout;using std::endl;

int main() {int a = 10;int b = 3;

cout << a/b;

  cout << a b;

Page 2: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 2/22

  return 0;}

31

1.1.4.Using the assignment operator

#include <iostream>using std::cout;using std::endl;

int main() {  int a = 10;  int b = 6;  int c = 3;  int d = 4;

  int f = (a + b)/(c + d);

  cout << f ;

  return 0;}

2

1.3.3.A scoping example

#include <iostream>

using std::cout;using std::endl;

 void useLocal( void ); void useStaticLocal( void ); void useGlobal( void );

int x = 1;

int main(){  int x = 5;

  cout << x << endl;  {  int x = 7;  cout << "local x in main's inner scope is " << x << endl;  }

  cout << x << endl;

  useLocal();useStaticLocal();

Page 3: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 3/22

  useGlobal();useLocal();useStaticLocal();useGlobal();

cout << x << endl;

 return

0;}

 void useLocal( void ){  int x = 25;

  cout << "local x is " << x << " on entering useLocal" << endl;  x = x + 20;  cout << "local x is " << x << " on exiting useLocal" << endl;}

 void useStaticLocal( void ){

  static int x = 50;

  cout << "local static x is " << x << " on entering useStaticLocal"<< endl;

  x = x + 20;  cout << "local static x is " << x << " on exiting useStaticLocal"

<< endl;}

 void useGlobal( void ){  cout << "global x is " << x << " on entering useGlobal" << endl;  x = x + 20;

  cout << "global x is " << x << " on exiting useGlobal" << endl;}

5local x in main's inner scope is 75local x is 25 on entering useLocallocal x is 45 on exiting useLocallocal static x is 50 on entering useStaticLocallocal static x is 70 on exiting useStaticLocalglobal x is 1 on entering useGlobalglobal x is 21 on exiting useGloballocal x is 25 on entering useLocallocal x is 45 on exiting useLocallocal static x is 70 on entering useStaticLocallocal static x is 90 on exiting useStaticLocalglobal x is 21 on entering useGlobalglobal x is 41 on exiting useGlobal5

1.3.4.Using the unary scope resolution operator

Page 4: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 4/22

#include <iostream>using std::cout;using std::endl;

int n = 7;

int main(){  double n = 10.5;

  cout << "Local double value of n = " << n  << "\nGlobal int value of n = " << ::n << endl;  return 0;}

Local double value of n = 10.5Global int value of n = 7

1.3.5.Finding maximum and minimum values for data types

#include <limits>#include <iostream>using std::cout;using std::endl;using std::numeric_limits;

int main() {  cout << "The range for type short is from "  << numeric_limits<short>::min()  << " to "  << numeric_limits<short>::max();  cout << "The range for type int is from "  << numeric_limits<int>::min()  << " to "  << numeric_limits<int>::max();  cout << "The range for type long is from "  << numeric_limits<long>::min()  << " to "  << numeric_limits<long>::max();  cout << "The range for type float is from "  << numeric_limits<float>::min()  << " to "  << numeric_limits<float>::max();  cout << "The range for type double is from "

  << numeric_limits<double>::min()  << " to "  << numeric_limits<double>::max();  cout << "The range for type long double is from "  << numeric_limits<long double>::min()  << " to "  << numeric_limits<long double>::max();  cout << endl;  return 0;

Page 5: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 5/22

}

The range for type short is from -32768 to 32767The range for type int is from -2147483648 to 2147483647The range for type long is from -2147483648 to 2147483647The range for type float is from 1.17549e-038 to 3.40282e+038

The range for typedouble is from 2.22507e-308 to 1.79769e+308The range for type long double is from 0 to 1.#INF

1.3.6.Comparing data values

#include <iostream>using std::cin;using std::cout;using std::endl;

int main() {

  char first = 10;  char second = 20;

  cout << "The value of the expression first < second is: "  << (first < second)  << endl  << "The value of the expression first == second is: "  << (first == second)  << endl;

  return 0;}

The value of the expression first < second is: 1The value of the expression first == second is: 0

1.4.1.Variable size: int, short, long, char, float and double

#include <iostream> int main() {  std::cout << "The size of an int is:\t\t";  std::cout << sizeof(int) << " bytes.\n";  std::cout << "The size of a short int is:\t";

std::cout << sizeof(short) << " bytes.\n";  std::cout << "The size of a long int is:\t";  std::cout << sizeof(long) << " bytes.\n";  std::cout << "The size of a char is:\t\t";  std::cout << sizeof(char) << " bytes.\n";  std::cout << "The size of a float is:\t\t";  std::cout << sizeof(float) << " bytes.\n";  std::cout << "The size of a double is:\t";  std::cout << sizeof(double) << " bytes.\n";

Page 6: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 6/22

 return 0;

 }

The size of an int is: 4 bytes.The size of a short int is: 2 bytes.The size of a long int is: 4 bytes.

The size of a char is: 1 bytes.The size of a float is: 4 bytes.The size of a double is: 8 bytes.

1.5.global variable1.5.1.Use a global variable1.5.2.Global class variable1.5.3.Global variables are known throughout the entire program and may be used by any p1.5.4.Use :: to reference global variable

1.5.1.Use a global variable

#include <iostream>using namespace std;

 void func1(); void func2();

int count; // This is a global variable.

int main(){  int i; // This is a local variable

  for(i=0; i<10; i++) {  ::count = i * 2;  func1();  }

  return 0;}

 void func1(){  cout << "count: " << ::count; // access global count  cout << '\n';  func2();

}

 void func2(){  int count;

  for(count=0; count<3; count++)  cout << '.';}

Page 7: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 7/22

count: 0...count: 2...count: 4...count: 6...count: 8...count: 10...count: 12...count: 14...count: 16...count: 18...

1.5.2.Global class variable

#include<iostream.h>

class test{

  int i; public:  test()  {  i=25;  for(int ctr=0; ctr<10;ctr++)  {  cout<<"Counting at"<<ctr<<"\n";  }  }

  ~test(){};};

test anObject;

main(){  return 0;}

Counting at0Counting at1Counting at2Counting at3Counting at4Counting at5Counting at6Counting at7Counting at8Counting at9

1.5.3.Global variables are known throughout the entire program and may be usedby any piece of code.

Page 8: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 8/22

#include <stdio.h>

 void func1( void ), func2( void );

int count;

main( void 

){count = 100;func1();return 0;

}

 void func1( void ){int temp;

temp = count;func2();printf("count is %d", count); /* will print 100 */

}

 void func2( void ){int count;

for(count=1; count<10; count++)putchar('.');

}

1.5.4.Use :: to reference global variable

#include <iostream>using namespace std;int global_name = 1001;

int main( void ){  int global_name = 1; // Local variable

  cout << "Local variable value " << global_name << '\n';  cout << "Global variable value " << ::global_name << '\n';}

1.6.block scope variable

1.6.1.Variable block scope

Page 9: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 9/22

1.6.2.Variables can be local to a block

1.6.3.Inner block variable scope

1.6.4.Names in inner scopes can hide names in outer scopes.

1.6.5.Using the scope resolution operator: '::'

1.6.6.global and block scope

1.6.7.scope code block

1.6.8.global variables across functions

1.6.9.Using the scope resolution operator (2)

1.6.1.Variable block scope

#include <iostream>using std::cout;using std::endl;

int main() {  int count1 = 10;  int count3 = 50;

  cout << endl << "Value of outer count1 = " << count1;

  {  int count1 = 20;  int count2 = 30;  cout << endl << "Value of inner count1 = " << count1;  count1 += 3;  count3 += count2;  }

  cout << endl  << "Value of outer count1 = " << count1  << endl  << "Value of outer count3 = " << count3;

  cout << endl;  return 0;}

Value of outer count1 = 10

Value of inner count1 = 20Value of outer count1 = 10Value of outer count3 = 80

1.6.2.Variables can be local to a block

#include <iostream>

Page 10: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 10/22

using namespace std;

int main() {int x = 19; // x is known to all code.

if(x == 19) {int

y = 20;

cout << "x + y is " << x + y << "\n";}

// y not known here.

return 0;}

x + y is 39

1.6.3.Inner block variable scope

#include <iostream>using namespace std;

int main(){int i;int j;

i = 10;j = 100;

if(j > 0) {

int i; // this i is separate from outer i

i = j / 2;cout << "inner i: " << i << '\n';

}

cout << "outer i: " << i << '\n';

return 0;}

inner i: 50outer i: 10

1.6.4.Names in inner scopes can hide names in outer scopes.

#include <iostream>

Page 11: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 11/22

#include <ostream>

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

  int x = 2;  if (x < i)  {  double x = 3.4;  std::cout << x;

}  std::cout << x;}

  //std::cout << x; // Error: no x declared in this scope}

2223.423.423.423.423.423.423.42

1.6.5.Using the scope resolution operator: '::'

#include <iostream>using std::cout;using std::endl;

int count1 = 100;

int main() {int count1 = 10;

  int count3 = 50;  cout << endl << "Value of outer count1 = " << count1;

  cout << endl << "Value of global count1 = " << ::count1;

  {int count1 = 20;

  int count2 = 30;  cout << endl << "Value of inner count1 = " << count1;  cout << endl << "Value of global count1 = " << ::count1;  count3 += count2;  }

cout << endl  << "Value of outer count1 = " << count1  << endl  << "Value of outer count3 = " << count3;

  cout << endl;  return 0;}

Value of outer count1 = 10Value of global count1 = 100Value of inner count1 = 20Value of global count1 = 100Value of outer count1 = 10

Page 12: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 12/22

Value of outer count3 = 80

1.6.6.global and block scope

#include <iostream.h>

int n=0; //Global

main(){  int n = 1;  {  int n = 2 ;  {  int n = 3;  cout << "In inner n=" <<n<<endl;  cout << "Global n=" << ::n <<endl;  }

  cout << "In outter n=" <<n<<endl;  cout << "Global n=" <<::n<<endl;  }  cout << "In main() n=" << n<<endl;  return 0 ;}

In inner n=3Global n=0In outter n=2Global n=0In main() n=1

1.6.7.scope code block 

#include <iostream>

using namespace std;

 void func();

int main(){  int var = 5;  cout << "In main() var is: " << var << "\n\n";

  func();

  cout << "Back in main() var is: " << var << "\n\n";  {  cout << "In main() in a new scope var is: " << var << "\n\n";  cout << "Creating new var in new scope.\n";  int var = 10;  cout << "In main() in a new scope var is: " << var << "\n\n";

Page 13: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 13/22

  }

  cout << "At end of main() var is: " << var << "\n";

  return 0;}

 void func(){  int var = -5; // local variable in func()  cout << "In func() var is: " << var << "\n\n";}

1.6.8.global variables across functions

#include <iostream>

using namespace std;

int glob = 10; // global variable

 void access_global(); void hide_global(); void change_global();

int main(){  cout << "In main() glob is: " << glob << "\n\n";  access_global();

  hide_global();  cout << "In main() glob is: " << glob << "\n\n";

  change_global();  cout << "In main() glob is: " << glob << "\n\n";

  return 0;}

 void access_global(){  cout << "In access_global() glob is: " << glob << "\n\n";}

 void hide_global()

{  int glob = 0; // hide global variable glob  cout << "In hide_global() glob is: " << glob << "\n\n";}

 void change_global(){  glob = -10; // change global variable glob  cout << "In change_global() glob is: " << glob << "\n\n";

Page 14: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 14/22

}

1.6.9.Using the scope resolution operator (2)

#include <iostream>#include <ostream>

namespace n {  struct counter {  static int n;  };  double n = 2.8;}

int n::counter::n = 7;

int main()

{  int counter = 0;int n = 10;

std::cout << n::counter::n;std::cout << n::n;std::cout << x.n;std::cout << n;std::cout << counter;

}

Value of outer count1 = 10Value of global count1 = 100Value of inner count1 = 20

Value of global count1 = 100Value of outer count1 = 10Value of outer count3 = 80

1.7.1.Use function in an expression

#include <iostream>using namespace std;

double box(double length, double width, double height); // use double data

Page 15: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 15/22

 int main(){double sum;

sum = box(10.1, 11.2, 3.3) + box(5.5, 6.6, 7.7) + box(4.0, 5.0, 8.0);

cout << "The sum of the volumes is " << sum << "\n";cout << "The average volume is " << sum / 3.0 << "\n";

return 0;}

// This version of box uses double data.double box(double length, double width, double height){return length * width * height ;

}

The sum of the volumes is 812.806

The average volume is 270.935

1.8.const1.8.1.Define constant1.8.2.Demonstrates constants1.8.3.const double value1.8.4.Using a properly initialized constant variable.1.8.5.Same Program Using Constant Integers1.8.6.Demonstrating the const type qualifier

Page 16: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 16/22

1.8.1.Define constant

#include <iostream>using namespace std;

const int intCount = 100;

int main(){int empNums[intCount];double salary[intCount];char *names[intCount];}

1.8.2.Demonstrates constants

#include <iostream>using namespace std;

int main(){  const int ALIEN_POINTS = 150;  int aliensKilled = 10;  int score = aliensKilled * ALIEN_POINTS;  cout << "score: " << score << endl;

  enum difficulty {NOVICE, EASY, NORMAL, HARD, UNBEATABLE};  difficulty myDifficulty = EASY;

  enum ship {FIGHTER = 25, BOMBER, CRUISER = 50, DESTROYER = 100};  ship myShip = BOMBER;  cout << (CRUISER - myShip) << " Resource Points.\n";  return 0;}

1.8.3.const double value

#include <iostream>

using namespace std;

int main(){

double bottles;

  cout << "How many bottles do you have? ";  cin >> bottles;

  double cans;

Page 17: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 17/22

  cout << "How many cans do you have? ";  cin >> cans;

  const double BOTTLE_VOLUME = 2.0;  const double CAN_VOLUME = 0.355;  double total = bottles * BOTTLE_VOLUME

+ cans * CAN_VOLUME;

  cout << "The total volume is " << total << " liter.\n";

  return 0;}

1.8.4.Using a properly initialized constant variable.

#include <iostream>using std::cout;using std::endl;

int main(){  const int x = 7;

  cout << x << endl;

  return 0;}

7

1.8.5.Same Program Using Constant Integers

#include <iostream>int main(){  const int Sunday = 0;  const int Monday = 1;  const int Tuesday = 2;  const int Wednesday = 3;  const int Thursday = 4;  const int Friday = 5;  const int Saturday = 6;

  int today;  today = Monday;

  if (today == Sunday || today == Saturday)  std::cout << "\nGotta' love the weekends!\n";  else

  std::cout << "\nBack to work.\n";

  return 0;}

Page 18: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 18/22

1.8.6.Demonstrating the const type qualifier

#include <iostream>using std::cout;using std::endl;

 void f( const int [] );

int main(){  int a[] = { 10, 20, 30 };

  f( a );  cout << a[ 0 ] << ' ' << a[ 1 ] << ' ' << a[ 2 ] << '\n';

  return 0;}

 void f( const int b[] )

{  cout << b[ 0 ];}

1010 20 30

1.12.static

1.12.1. Using a static long variable

1.12.2.

Use static variable to compute a running average of numbers entered

 by the user 1.12.3. Initialize static member field outside the class declaration

1.12.4.A static local variable causes the compiler to create permanent

storage for it in much the same way that it does for a global variable.

1.12.1.Using a static long variable

Page 19: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 19/22

#include <iostream>#include <iomanip>using std::cout;using std::endl;

long next();

int main() {  for(int i = 0 ; i < 30 ; i++) {  cout << std::setw(12) << next ();  }  cout << endl;  return 0;}

long next () {  static long last = 0;  last = last + 1;

return last;}

1 2 3 4 5 67 8 9 10 11 12 13

14 15 16 17 18 1920

21 22 23 24 25 2627 28 29 30

1.12.2.Use static variable to compute a running average of numbers entered by the user 

#include <iostream>using namespace std;

int f(int i);

int main(){int num;

do {cout << "Enter numbers (-1 to quit): ";cin >> num;if(num != -1)cout << "average is: " << f(num) << "\n";

} while(num > -1);

return 0;}

int f(int i){static int sum = 0, count = 0;

Page 20: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 20/22

 sum = sum + i;

count++;

return sum / count;

}Enter numbers (-1 to quit): 1average is: 1Enter numbers (-1 to quit): 2average is: 1Enter numbers (-1 to quit): 3average is: 2Enter numbers (-1 to quit): 2average is: 2Enter numbers (-1 to quit): 1average is: 1Enter numbers (-1 to quit): 2average is: 1

Enter numbers (-1 to quit): 3average is: 2Enter numbers (-1 to quit): -1

1.12.3.Initialize static member field outside the class declaration

#include <iostream>using std::cout;using std::endl;

class Box {  public:  Box() {  cout << "Default constructor called" << endl;  ++objectCount;  length = width = height = 1.0;  }

  Box(double lvalue, double wvalue, double hvalue) :

  length(lvalue), width(wvalue), height(hvalue){  cout << "Box constructor called" << endl;  ++objectCount;  }

  double volume() const {  return length * width * height;  }

Page 21: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 21/22

  int getObjectCount() const {return objectCount;}

  private:  static int objectCount;  double length;

  double width;  double height;};int Box::objectCount = 0;

int main() {  cout << endl;

  Box firstBox(17.0, 11.0, 5.0);  cout << "Object count is " << firstBox.getObjectCount() << endl;  Box boxes[5];  cout << "Object count is " << firstBox.getObjectCount() << endl;

  cout << "Volume of first box = "

  << firstBox.volume()  << endl;

  const int count = sizeof boxes/sizeof boxes[0];

  cout <<"The boxes array has " << count << " elements."  << endl;

  cout <<"Each element occupies " << sizeof boxes[0] << " bytes."  << endl;

  for(int i = 0 ; i < count ; i++)  cout << "Volume of boxes[" << i << "] = "

  << boxes[i].volume()  << endl;

  return 0;}

Box constructor calledObject count is 1Default constructor calledDefault constructor calledDefault constructor calledDefault constructor calledDefault constructor calledObject count is 6

Volume of first box = 935The boxes array has 5 elements.Each element occupies 24 bytes.Volume of boxes[0] = 1Volume of boxes[1] = 1Volume of boxes[2] = 1Volume of boxes[3] = 1Volume of boxes[4] = 1

Page 22: basics in c++

8/8/2019 basics in c++

http://slidepdf.com/reader/full/basics-in-c 22/22

1.12.4.A static local variable causes the compiler to create permanent storage for it in much the

same way that it does for a global variable.

#include <stdio.h>#include <conio.h>

int count(int i);

int main( void ){do {count(0);

} while(!kbhit());printf("count called %d times", count(1));return 0;

}

int count(int i){static int c=0;

if(i) return c;else c++;return 0;

}