Students

C++ Arrays

type array_name[size];

Consider this code.

int test[85];

Array Elements

int age[5];
Elements of an array in C++ Programming


Example 1: C++ Array

C++ Program to store 5 numbers entered by user in an array and display first and last number only.

#include <iostream>
using namespace std;

int main() {
    int n[5];
    cout<<"Enter 5 numbers: ";
/*  Storing 5 number entered by user in an array using for loop. */
    for (int i = 0; i < 5; ++i) {
        cin>>n[i];
    }
    
    cout<<"First number: "<<n[0]<<endl;  // first element of an array is n[0]
    cout<<"Last number: "<<n[4];        // last element of an array is n[SIZE_OF_ARRAY - 1]
    return 0;
}

C++ Multidimensional Arrays

int x[3][4];
Elements in two dimensional array in C++ Programming

Three dimensional also array works in similar way. For example:

float x[2][4][3];

Multidimensional Array Initialisation

int test[2][3] = {2, 4, -5, 9, 0, 9};
int  test[2][3] = { {2, 4, 5}, {9, 0 0}};

nitialisation of three dimensional array

int test[2][3][4] = {3, 4, 2, 3, 0, -3, 9, 11, 23, 12, 23, 
                 2, 13, 4, 56, 3, 5, 9, 3, 5, 5, 1, 4, 9};

Better way to initialise this array with same elements as above.

int test[2][3][4] = { 
                     { {3, 4, 2, 3}, {0, -3, 9, 11}, {23, 12, 23, 2} },
                     { {13, 4, 56, 3}, {5, 9, 3, 5}, {3, 1, 4, 9} }
                 };

Example 1: Two Dimensional Array

C++ Program to display all elements of an initialised two dimensional array.

#include <iostream>
using namespace std;

int main() {
    int test[3][2] = {
        {2, -5},
        {4, 0},
        {9, 1}
    };
    for(int i = 0; i < 3; ++i) {
        for(int j = 0; j < 2; ++j) {
            cout<< "test["<< i << "][" << ;j << "] = " << test[i][j]<<endl;
        }
    }
    return 0;
}

Example 3: Three Dimensional Array

C++ Program to Store value entered by user in three dimensional array and display it.

#include <iostream>
using namespace std;

int main() {
    int test[2][3][2];    // this array can store 12 elements
    cout<<"Enter 12 values: \n";
    for(int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            for(int k = 0; k < 2; ++k ) {
                cin>>test[i][j][k];
            }
        }
    }
    cout<<"\nDisplaying Value stored:"<<endl;
/* Displaying the values with proper index. */
    for(int i = 0; i < 2; ++i) {
        for (int j = 0; j < 3; ++j) {
            for(int k = 0; k < 2; ++k ) {
                cout<< "test["<<i<<"]["<<j<<"]["<<k<<"] = "<< test[i][j][k]<<endl;
            }
        }
    }

    return 0;
}




 

C++ Pointers [Basic Code]

/ my first pointer
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << '\n';
  cout << "secondvalue is " << secondvalue << '\n';
  return 0;
}

 C++ Structure

C++ Program to assign data to client /employee /student of a structure variable and display it.

#include <iostream>
using namespace std;

struct  person {
    char name[50];
    int age;
    float salary;
};

int main() {

    person p1;
    
    cout << "Enter Full name: ";
    cin.get(p1.name, 50);
    cout << "Enter age: ";
    cin >> p1.age;
    cout << "Enter salary: ";
    cin >> p1.salary;

    cout << "\nDisplaying Information." << endl;
    cout << "Name: " << p1.name << endl;
    cout <<"Age: " << p1.age << endl;
    cout << "Salary: " << p1.salary;

    return 0;
}

Output

Enter Full name: Muhammad Ahsan
Enter age: 25
Enter salary: 50,000

Displaying Information.
Name: Muhammad Ahsan
Age: 25
Salary: 50,000

C Programming Unions

Unions are quite similar to the structures in C. Union is also a derived type as structure. Union can be defined in same manner as structures just the keyword used in defining union in union where keyword used in defining structure was struct.

union car{
  char name[50];
  int price;
};

Union (variables) can be created like.

union house{
  char name[50];
  int price;
}c1, c2, *c3;

OR;

union house{
  char name[50];
  int price;
};
-------Inside Function-----------
union home c1, c2, *c3;


In both cases, union variables c1, c2 and union pointer variable c3 of type union car is created.

 

 

Object and Class



#include <iostream>
using namespace std;
class temp
{
    private:
        int data1;
        float data2;
    public:
       void int_data(int d){
          data1=d;
          cout<<"Number: "<<data1;
         }
       float float_data(){
           cout<<"\nEnter data: ";
           cin>>data2;
           return data2;
         }
};
 int main(){
      temp obj1, obj2;
      obj1.int_data(12345);
      cout<<"You entered "<<obj2.float_data();
      return 0;
 }

Output:

Number: 12345
Enter data: 
You entered:

Explanation of Program

In this program, two data members data1 and data2 and two member functionint_data() and float_data() are defined under temp class. Two objects obj1 and obj2 of that class are declared. Function int_data() for the obj1 is executed using codeobj1.int_data(12);, which sets 12 to the data1 of object obj1. Then, function float_data()for the object obj2 is executed which takes data from user; stores it in data2 of obj2and returns it to the calling function.

Note: In this program, data2 for object obj1 and data1 for object obj2 is not used and contains garbage value.

Data member according to Object in C++.

 

C++ Strings

 string or name ,sentence entered by user.

#include <iostream>
using namespace std;

int main() {
    char str[50];
    cout<<"Enter a string: ";
    cin>>str;
    cout<<"You entered: "<<str<<endl;
    cout<<"\nEnter another string: ";
    cin>>str;
    cout<<"You entered: "<<str<<endl;
    return 0;
}

Output

Enter a string: welcome Ahsan
You entered: welcome Ahsan

Enter another string: Inside Observer
You entered: Inside Observer