C++ - Array, Pointer , Object and Class

 

Arrays

 

Collection of identical data object which are stored in consecutive memory location under common name.


Syntax

Data type array-name[size]; 


Example

int a[10];

 

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int n[5]={10,20,30,40,50};

int i;

for(i=0;i<5;i++)

cout<<"Number"<<i+1<<"="<<n[i]<<endl;

getch();

}

 

Types of Arrays

 

          One-Dimensional Array

          Two-Dimensional Array

          Multi-Dimensional Array

 

Pointers

 

It holds the memory address of another variable

 

 

 

Declaration

 

Syntax

          data type *pointer-variable;

 

Example

int *p;

 

 

#include<iostream.h>

#include<conio.h>

void main()

{

clrscr();

int i,*p;

p=&i;

i=20;

cout<<"i value is"<<i<<endl;

*p=*p+30;

cout<<"i value is "<<i<<endl;

getch();

}

 

 

OBJECT AND CLASS

 

Class

 

·        It is an unit which combines both data and the functions that operates on the data

·        Internal data of the class member data

·        Internal function - member functions

 

Object

 

·        The variable of the class

·        Instances of a class

 

Class declaration and access specifiers

 

Syntax

 

Class user_defined_name

{

          <access_specifier>:data_type member_data;

Member functionns

Friend functions

}

 

Private

 

·        Member data can only be accessed by the member functions and friend of this class

·        Member functions and friends of this class can always read or write private data member

·        Not accessible from outside of the class

 

Public

 

·        Member accessed by any function in the outside of the class

·        Outside functions – Read and write the public member of the class

 

Protected

 

·        Members accessed by the member functions and friend functions and also by the member functions and friends derived from this class

·        Not accessible by the outside functions

 

Example

 

#include<iostream.h>

#include<conio.h>

class test

{

public:

int i,j,k;

};

void main()

{

test a,b;

a.i=10;

a.j=4;

clrscr();

a.k=a.i*a.j;

b.k=a.k*a.i;

cout<<a.k<<" "<<b.k;

getch();

}

No comments:

Post a Comment