GithubHelp home page GithubHelp logo

cpp-in-21-days-part4's Introduction

Cpp-in-21-days-Part4

Array

  • An array is a sequential collection of data storage locations, each of which holds the same type of sata
  • Array element offsets are counted from 0
  • SomeArray[n] has n elements that are numbered SomeArray[0] through SomeArray[n-1]
// Inialize an array
int myArray[5];                             // Empty Array of 5 integers
int IntegerArray[5] = {10, 20, 30, 40, 50};
int IntArray[] = {1, 2, 3, 4, 5, 6};        // Compiler automatically picks the array size!

int main()
{
enum WeekDays {Sun, Mon, Tue, Wed, Thu, Fri, Sat, DaysInWeek};
int ArrayWeek[DaysInWeek] = {10, 20, 30, 40, 50, 60, 70};
// ArrayWeek is declared to have DaysInWeek elements, which is 7
std::cout << "The value at Tuesday is: " << ArrayWeek[Tue];
return 0;
}

Writing Past the End of an Array

  • DON'T write value into an array past the initialized size! The program may crash
  • You cannot initialize more elements than you've declared for the array

Arrays of Objects

  • When you declare the array to hold objects, you tell the compiler the type of object to store and the number
  • The class MUST have a default constructor (takes no arguments) so that the object can be created

class Cat
{
public:
Cat() {itsAge = 1; itsWeight = 5;} // constructor
~Cat() {}
int GetAge() {return itsAge;}
int GetWeight() {return itsWeight;}
void SetAge(int age) {itsAge = age;}
private:
int itsAge;
int itsWeight;
};
int main()
{
Cat Litter[5]; // Array of 5 Cats
int i;
for (i=0; i<5; i++)
{
Litter[i].SetAge(2*i + 1);
}
for (i=0; i<5; i++)
{
cout << "Cat #" << i+1 << ": ";
cout << Litter[i].GetAge() << " years old" << endl;
}
return 0;
}

Multidimensional Arrays (Matrix)

Initializing Multidimensional Arrays

int theArray[5][3] = { {1,2,3},
  {4,5,6},
  {7,8,9},
  {10,11,12},
  {13,14,15}};   // for easy understanding

Array of Pointers

C-style string

  • An array of characters that is terminated by a null
  • Ex:
char Greeting[] =
{'H','e','l','l','o', ' ', 'W','o','r','l','d','\0'};
// This is equivalent to
char Greeting2[] = "Hello World";
  • The last character, \0, is the null character, telling c++ to terminate
  • For double-quoted string, the compiler automatically add the null character for you
  • Hello World is 12 bytes: Hello=5 bytes, space=1byte, World=5 bytes, and the null character is 1 byte

strcpy() and strncpy()

  • strcpy(String2, String1) copies the entire contents of one string into a designated buffer
  • strncpy(String2, String1, MaxLength) copies a number of characters from one string to another
  • If the source is larger than the destination, strcpy() overwrites past the end of the buffer

// Listing 13.12 - Using strcpy()
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char String1[] = "No man is an island";
char String2[80];
strcpy(String2, String1);
cout << "String1: " << String1 << endl;
cout << "String2: " << String2 << endl;
return 0;
}

// Listing 13.13 - Using strncpy() (P.436)
#include <iostream>
#include <string.h>
int main()
{
const int MaxLength = 80;
char String1[] = "No man is an island";
char String2[MaxLength +1];
strncpy(String2, String1, MaxLength);
std::cout << "String1: " << String1 << std::endl;
std::cout << "String2: " << String2 << std::endl;
return 0;
}

cpp-in-21-days-part4's People

Contributors

komxun avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.