Arrays :

    • Array is an concept for storage and printing purpose.
    • Array is an collection of similar data types which referred by unique name .
    • One variable can store only one value at time but if we want to store multiple values of same data type at same time , then we can use concept array .
    • Depending upon size of array , it has two types .

Types of array :
 





                                                    

 

 


Types of array are depend upon number of subscripts [] 

One dimensional array :


           The array which has only one subscript is called as one dimensional array .

Syntax :

data_type array_name[size]={elements};

Example :

int tp[5]={1,2,3,4,5,6};

Q. Write c++ program to print one dimensional array


#include<iostream.h>
void main()
{
int t,arr[4];
cout<<"enter values for array";
for(t=0;t<4;t++)
cin>>arr[t];
cout<<"Array is : ",arr[t]<<endl;
getch();
}


Two dimensional array :

  • It is also called as matrix .
  • The array which has two subscripts is called as two dimensional array .
  • In this array first subscript is for row and second is for columns ;

Syntax :

data_type array_name[ROW][COL]={elements};

Example :

data_type array_name[2][2]={{1,2},{3,4}};

Q, Write a c++ program for addition of two matrices

 #include<iostream.h>

void main()

{

int t,p,mat1[2][2],mat2[t][p],mat3[2][2];

cout<<"Enter elements for mat1 :";

for(t=0;t<2;t++)

for(p=0;p<2;p++)

cin>>mat1[t][p];

cout<<"Enter elements for mat2 :";

for(t=0;t<2;t++)

for(p=0;p<2;p++)

cin>>mat2[t][p];

cout<<"Addition is :"

for(t=0;t<2;t++)

{

for(p=0;p<2;p++)

mat3[t][p]=mat1[t][p]+mat2[t][p];

}

}


Structure :

  • It is an array of different data types or collection of different data types .
  • We can store multiple elements at a time .

Syntax :

struct structure_name

{

             Datatype member1:

             Datatype  member2;

            …………

};  // object for structure class is created here .


Example :

struct student

{

int rollno;

char name[10];

float marks;

};


Q. Write a c++ program to print student data using structure .

#include<iostream.h>

Struct student

{

int rollno;

char name[10];

float marks;

};

void main()

student s1;

cout<<”enter data of student:\n”;

cout<<”roll number:”;

cin>>s1.rollno;

cout<<”name”;

cin>>s1.name;

cout<<”marks”;

cin>>s1.marks;

cout<<”student details\n”;

cout<<”roll number:”<<s1.rollno<<endl;

cout<<”name:”<<s1.name<<endl;

cout<<”marks:”<<s1.marks<<endl;

}

 

Output:

Enter data of student

Roll number: 01

Name: tprograming

Marks: 98

Student details

Roll number: 01

Name: tprograming

Marks:  98 


Class declarations :

class class_name

(

private:

               variables declarations;

                function declarations

public:

variables declarations;

function declarations;

}; 

 

Example : 

#include<iostream.h>

class stud

{

private:

int rollno;

char name[20];

public:

void getdata()

{

cout<<"Enter roll number of student:";

cin>>rollno;

cout<<"Enter name of student”;

cin>>name;

}

void putdata()

{

cout<<"Roll Number: "<<rollno<<endl;

cout<<"Name:"<<name<<endl;

}

};

void main()

{

stud s1;

s1.getdata();

s1.putdata():

}

Output:

Enter roll number of student: 01

Enter name of student: Mohini

Roll Number: 01

Name: Mohini

Example : by passing arguments 

#include<string.h>

#include<iostream.h>

class student

{

private:

int rollno;

char name[20];

public:

void setdata(int rno, char *na)

{

rollno =rno;

strcpy(name,na);

}

Void putdata()

{

cout<<"Roll Number: "<<rollno<<endl; 

cout<<"Name:"<<name<<endl;

}

};

void main()

{

student s1;

s1. setdata(301,"Vinod");

s1.putdata();

}

Output

Roll Number: 01

Name: tprograming


Constructors :

    • It is used to initialize an object .
    • Constructors are used pass values .
    • Constructor name and class name are always same .



Default constructor -

                                It is an empty constructor which didn't passes parameters inside it .

Example :

#include<iostream.h>

#include<string.h>

class student

{

private:

int rollno,

char name[20];

public:

student()                         //default constructor

{

rollno=01;

strcpy(name, "Tprograming");

}

void putdata()

{

cout<<"Roll Number:"<<rollno<<endl; 

cout<<"Name:"<<<name<<endl;

}

};

void main()

{

student s1;

cout<<"Values due to constructors: "<<endl;

s1.putdata():

}

Output:

Values due to constructors:

Roll Number: 01

Name: Tprograming


Parameterized constructor / Overloaded constructor : 

A constructor having parameters is called as parameterized constructor .

A class can have more than one constructors is called as overloaded constructor .

Syntax :

constructor_name(Parameter_list)

{

}

Example :

Q. Write c++ program to demonstrate Parameterized or Overloaded constructor .

#include<iostream.h>

#include<string.h>

class student

{

private:

int rollno;

char name[20];

public:

student()                                                       //default constructor

{

}

student(int rno,char tname[])                      //parameterized constructor

{

rollno=rno;

strcpy(name.tname);

}

void getdata()

{

cout<<"Enter your rollno : ";

cin>>rollno;

cout<<"Enter your name : ";

cin>>name;

}

void putdata()

{

cout<<"Roll Number : "<<rollno<<endl;

cout<<"Name : "<<name<<endl;

}

};

void main()

{

student s1("Tprograming",01);

student s2;

s2.getdata();

cout<<"Date of first student : \n";

s1.putdata();

cout<<"Data of second student : \n";

s2.putdata();

}


Copy constructor :

           It is used to make copy of one object from another .

           & symbol is used to give reference of copy constructor .

Syntax:

constructor_name(const class_name &variable);


Example :

#include<iostream.h>

class student

{

private:

int age;

char name[20];

public:

student()

{

}

student(int tage,char tname[])

{

age=tage;

strcpy(name.tname);

}

student(const student &x)

{

age=x.age;

strcpy(name,x.name);

}

void putdata()

{

cout<<"Name : "<<name;

cout<<"Age : "<<age;

}

void main()

{

student s1;

cout<<"Object created by default constructor : ";

s1.display();

student s2("Tprograming",21);

cout<<"Object created by parameterized constructor : ";

s2.display();

student s3(s2);

cout<<"Object created with object as parameter :";

student s3.display;

}


Q.What is static data class ?

Static keyword is used here .

Only one copy of object is created for entire class and it shared by all other members.

Example :

#include<iostream.h>

class tpro

{

private:

static int no;

int num;

public:

void getdata(int a)

{

num=a;

no++;

}

void showcount()

{

cout<<"no="<<no<<endl;

}

};

int number::no;

void main()

{

number n1,n2;

n1.showcount();

n2.showcount();

n1.getdata();

n2.getdata();

cout<<"values : "<<endl;

n1.showcount();

n2.showcount();

}


String :

Let's learn it .

a=10

In the above line we have stored only one value in variable a .

If we want to store multiple values at a time then we have option array .

String is an array of characters or collection of characters .

Syntax :

char string_name[size]='' characters"

char string_name[size]={'a','b','c'};


Example :

#include<iostream.h>

void main()

{

char name[50];

cout<<"Enter your name : ";

cin>>name;

cout<<"Name is : "name<<endl

}

Q. What is reading embedded blanks in c++?

The insertion operator >> is used to read data.

If we want to skip it we used cin.get() function and this is called reading embedded blanks .

Example :

#include<iostream.h>

const int MAX=80

void main()

{

char str[MAX];

cout<<"Enter string : "

cin.get(str,MAX);

cout<<"string : "<<st;

}