C++ Learning (1)

Compile and execute C++

  • Type ‘g++ hello.cpp ’ and press enter to compile your code. If there are no errors in your code the command prompt will take you to the next line and would generate a.out executable file.

-Now, type ‘./a.out’ to run your program.

$ g++ hello.cpp
$ ./a.out

Types

variable declaration, definition, initialisation.

definition

A variable definition means to tell the compiler where and how much to create the storage for the variable. e.g.:

int i, j, k;
char c, ch;

declaration

A variable declaration provides assurance to the compiler that there is one variable existing with the given type and name so that compiler proceed for further compilation without needing complete detail about the variable.

A variable declaration is useful when you are using multiple files and you define your variable in one of the files which will be available at the time of linking of the program. You will use extern keyword to declare a variable at any place.

#include <iostream>
using namespace std;

// Variable declaration:
extern int a, b;
extern int c;
extern float f;

int main ()
{
  // Variable definition:
  int a, b;
  int c;
  float f;

  // actual initialization
  a = 10;
  b = 20;
  c = a + b;

  cout << c << endl ;

  f = 70.0/3.0;
  cout << f << endl ;

  return 0;
}

Variables

Local Variables

Variables that are declared inside a function or block are local variables.

Global Variables

Global variables are defined outside of all the functions, usually on top of the program.

Constant

//#define identifier value

#define LENGTH 10   
#define WIDTH  5
#define NEWLINE '\n'

or

const int  LENGTH = 10;
const int  WIDTH  = 5;

auto/static/register

  • The auto storage class is the default storage class for all local variables.
  • The register storage class is used to define local variables that should be stored in a register instead of RAM(). It should also be noted that defining ‘register’ does not mean that the variable will be stored in a register
  • The static storage class instructs the compiler to keep a local variable in existence during the life-time of the program instead of creating and destroying it each time it comes into and goes out of scope. Therefore, making local variables static allows them to maintain their values between function calls.
#include <iostream>

// Function declaration
void func(void);

static int count = 10; /* Global variable */

main()
{
    while(count--)
    {
       func();
    }
    return 0;
}
// Function definition
void func( void )
{
    static int i = 5; // local static variable
    i++;
    std::cout << "i is " << i ;
    std::cout << " and count is " << count << std::endl;
}
  • The extern storage class is used to give a reference of a global variable that is visible to ALL the program files. When you use ‘extern’ the variable cannot be initialised as all it does is point the variable name at a storage location that has been previously defined. “extern” will be used in another file to give reference of defined variable or function.

In main.cpp

#include <iostream>
int count ;
extern void write_extern();

main()
{
   count = 5;
   write_extern();
}

In support.cpp:

#include <iostream>

extern int count;

void write_extern(void)
{
   std::cout << "Count is " << count << std::endl;
}

Arrays

type arrayName [ arraySize ];

double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
Multi-dimensional array: a[i][j];
Pointer array
double balance[50];
double *p;
p = balance;

//balance is a pointer to &balance[0]
/*
It is legal to use array names as constant pointers, and vice versa. Therefore, *(balance + 4) is a legitimate way of accessing the data at balance[4] also *(p+4) 
*/
Passing array to functions

C++ does not allow to pass an entire array as an argument to a function.

(1). Formal parameters as a pointer as follows

void myFunction(int *param)
{ //...
}

(2). Formal parameters as a sized array:

void myFunction(int param[10])
{ //...
}

(3). Formal parameters as an unsized array:

void myFunction(int param[])
{//...
}

As you can see, the length of the array doesn’t matter as far as the function is concerned because C++ performs no bounds checking.

Returning array from an function

C++ does not allow to return an entire array as an argument to a function.

  1. If you want to return a single-dimension array from a function, you would have to declare a function returning a pointer.
  2. Remember that C++ does not advocate to return the address of a local variable to outside of the function so you would have to define the local variable as static variable.
int * myFunction()
{//...
}
/*
Generate 10 random numbers and return them using an array and call this function as follows
*/
#include <iostream>
#include <ctime>

using namespace std;

// function to generate and retrun random numbers.
int * getRandom( )
{
  static int  r[10];

  // set the seed
  srand( (unsigned)time( NULL ) );
  for (int i = 0; i < 10; ++i)
  {
    r[i] = rand();
    cout << r[i] << endl;
  }

  return r;
}

// main function to call above defined function.
int main ()
{
   // a pointer to an int.
   int *p;

   p = getRandom();
   for ( int i = 0; i < 10; i++ )
   {
       cout << "*(p + " << i << ") : ";
       cout << *(p + i) << endl;
   }

   return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值