C++ Programming Fundamentals: Arrays, References, Structures, and More

Arrays can only contain the same data type. Data types: bool, char, int, float, double. To declare an array: type arrayName [ arraySize ]; — This is called a single-dimension array. To declare a 10-element array called balance of type double, use this statement:

Double balance[10];


You can initialize C++ array elements either one by one or using a single statement:

Double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};


— Or you can do this this way:


Double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};


— Ways to pass on an array to a function:


Void myFunction(int *param){ … } or void myFunction(int param[10]) { … } or void myFunction(int param[]) { … }


A reference variable is an alias, that is, another name for an already existing variable. You cannot have NULL references.
You must always be able to assume that a reference is connected to a legitimate piece of storage. Once a reference is initialized to an object, it cannot be changed to refer to another object. Pointers can be pointed to another object at any time. A reference must be initialized when it is created. Pointers can be initialized at any time. Let us say int    i = 17;

Then if you want to declare a reference, you declare it like this:


Int&    r = i;


— Calling a reference:

#include <iostream> using namespace std; // function declaration void swap(int& x, int& y); int main () {   // local variable declaration:   int a = 100; int b = 200;

Cout

<< “Before swap, value of a :” << a << endl; cout << “Before swap, value of b :” << b << endl;  /* calling a function to swap the values.*/ swap(a, b);

Cout


—— The C-Style Character String:

this string is actually a one-dimensional array of characters which is terminated by a null character ‘\0’. Either declared as char greeting[6] = {‘H’, ‘e’, ‘l’, ‘l’, ‘o’, ‘\0’};
or char greeting[] = “Hello”;

—- String functions:

strcpy(s1, s2); copies string s2 into string s1, strcat(s1, s2); concatenates string s2 onto the end of string s1, strlen(s1); returns the length of string s1, strcmp(s1, s2); returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2, strchr(s1, ch); returns a pointer to the first occurrence of character ch in string s1, strstr(s1, s2); returns a pointer to the first occurrence of string s2 in string s1. ———-Defining a Structure: struct [structure tag] { member definition; member definition; … Member definition; } [one or more structure variables]; —– struct Books { char  title[50]; char  author[50]; char  subject[100]; int   book_id; } book; You can use typedef keyword for non-structs as well as follows:typedeflongint*pint32; Using Structure Pointer: #include<iostream>#include<cstring>usingnamespace std;void printBook(structBooks*book );structBooks{char  title[50];char  author[50];char  subject[100];int   book_id; };int main(){structBooksBook1;// Declare Book1 of type BookstructBooksBook2;// Declare Book2 of type Book// Book 1 specification strcpy(Book1.title,“Learn C++ Programming”); strcpy(Book1.author,“Chand Miyan”);  strcpy(Book1.subject,“C++ Programming”);Book1.book_id =6495407;// Book 2 specification strcpy(Book2.title,“Telecom Billing”); strcpy(Book2.author,“Yakit Singha”); strcpy(Book2.subject,“Telecom”);Book2.book_id =6495700;// Print Book1 info, passing address of structure printBook(&Book1);// Print Book1 info, passing address of structure printBook(&Book2);return0;}// This function accept pointer to structure as parameter.void printBook(structBooks*book ){ cout <<“Book title : “<< book->title <<endl; cout <<“Book author : “<< book->author <<endl; cout <<“Book subject : “<< book->subject <<endl; cout <<“Book id : “<< book->book_id <<endl;} —— Question and Answers: What is wrong with the following definition for a C-style symbolic constant called PI?: #define PI = 3.24159 (answer: There should not be an equal sign) —
Which of the following (a-c) is a control structure?
(answer: sequence, repetition, and decision) —
How many times will the following while loop print “hello”? Int i = 1; while( i <= 10 ) cout << “hello” (answer:
an infinite number of times

) —

A function prototype does not have to (answer: include argument names) —
What will be displayed by the following code? Int main() { int num = 3; fn(num);  cout << num;
return 0; } void fn( int n ) {  n++;  } (answer = 3) —
Which of the following string functions will ALWAYS produce a valid string after execution?
(answer: strcpy and strcat) —
The :: operator is used to associate a method with a specific class

. (answer: true) —

Which of the following statements (a-d) about classes is true?
(answer: A class can contain both data members and methods, The members of a class may be private or public, The name of the constructor is always the same as the name of the class, The class definition contains prototype statements for the methods) — An instance of a class is known as a/an (answer: object) —
The class X contains two data members: an integer called xVal and a float called xPerc. Which of the following is not a valid prototype for the class definition for X? (answer: void X( int, float ); —