•A program with class
•A program with class
•Array of objects
•Concept of reference
•Dynamic memory allocation
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
A program with class
class point
{
int x,y; //data members by default private
public:
void input(int a, int b)
{
{
x=a;
y=b;
}
void output(void)
{
cout<<“x=”<<x<<“n”; //print value of x on output screen
cout<<“y=”<<y;
}
};
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
A program with class Cont..
int main()
{
point p1, p2; // object creation
p1.input(25,30);// member function calling
p2.input(55,60);
p1.output(); //print values of x and y for object p1
p1.output(); //print values of x and y for object p1
p2.output();
return 0;
}
Output:
x=25
y=30
x=55
y=60
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Array of objects
 Array: Collection of elements of same data type.
 If elements are of class type, then it is called array of objects.
 Example-
class employee
{
{
char name[20];
int age;
public:
void getdata( );
void putdata( );
};
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Array of objects
 Class “employee” used to create different categories of
employees. Example-
employee faculty[3]; // array of faculty
employee staff[5]; // array of staff
employee staff[5]; // array of staff
faculty[i].getdata(); // input data for ith faculty
staff[i].getdata(); // input data for ith staff
faculty[i]. putdata(); // display data of ith faculty
staff[i].putdata(); // display data of ith staff
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Concept of reference variables
 provides an alias (alternative name) for a previously
defined variable.
 Syntax:
data_type & reference_name=variable_name;
data_type & reference_name=variable_name;
 Example-
int x = 10;
int & y = x; // y is reference variable for x;
 Both variables refer to the same memory location.
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Concept of reference variables
cout<<x; // print 10
cout<<y; // print 10
x= x+5;
cout<<x; // print 15
cout<<x; // print 15
cout<<y; // print 15
y=0;
cout<<x; // print 0
cout<<y; // print 0
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Application of reference variables
 In passing arguments to functions.
 Example-
void swap(int x, int y) //swap values of two variables
{ int t=x; x=y; y=t;}
{ int t=x; x=y; y=t;}
int main()
{
int a=5, b=10;
swap(a,b); // function call by value
cout<<a<<b; // print 5 and 10
return 0;
}
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Application of reference variables
void swap(int &x, int &y) //swap values of two variables
{ int t=x; x=y; y=t;}
int main()
{
{
int a=5, b=10;
swap(a,b); // function call by reference
cout<<a<<b; // print 10 and 5
return 0;
}
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Dynamic memory allocation
 Memory allocated at run-time.
 “new” operator for memory allocation
 “delete” operator to free the allocated memory
Syntax:
 Syntax:
pointer-variable = new datatype;
 Example-
int *p=new int; // allocate integer size memory
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Dynamic memory allocation
 *p = 10; // assign value 10 to newly created
memory
 int *p=new int(10);// initialize the memory with value
10
10
 new can be used to create a memory space for any data
type including user defined such as arrays, structures,
and classes.
 int *p=new int[15];// creates memory for an array of 15
integer elements
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Dynamic memory allocation
 For multi-dimensional arrays, all the array sizes must
be supplied.
 Example-
int *q=new int[5][3][4];// legal
int *q=new int[5][3][4];// legal
int *q=new int[m][3][4];// first dimension may be
variable, all other must be constant.
int *q=new int[5][3][ ];// illegal
int *q=new int[ ][3][4];// illegal
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Dynamic memory allocation
 When allocated memory is no longer needed, it is
destroyed to release the memory space for reuse.
 Syntax-
delete pointer-variable;
delete pointer-variable;
 Example-
delete p; // free the memory pointed by pointer p.
delete [ ] q;// free the entire array pointed by pointer q.
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.
Thank You
Prepared by: Anil Kumar Tailor, Assistant Prof.,
Engineering College Ajmer.

array of objects slides.pdf

  • 1.
    •A program withclass •A program with class •Array of objects •Concept of reference •Dynamic memory allocation Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 2.
    A program withclass class point { int x,y; //data members by default private public: void input(int a, int b) { { x=a; y=b; } void output(void) { cout<<“x=”<<x<<“n”; //print value of x on output screen cout<<“y=”<<y; } }; Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 3.
    A program withclass Cont.. int main() { point p1, p2; // object creation p1.input(25,30);// member function calling p2.input(55,60); p1.output(); //print values of x and y for object p1 p1.output(); //print values of x and y for object p1 p2.output(); return 0; } Output: x=25 y=30 x=55 y=60 Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 4.
    Array of objects Array: Collection of elements of same data type.  If elements are of class type, then it is called array of objects.  Example- class employee { { char name[20]; int age; public: void getdata( ); void putdata( ); }; Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 5.
    Array of objects Class “employee” used to create different categories of employees. Example- employee faculty[3]; // array of faculty employee staff[5]; // array of staff employee staff[5]; // array of staff faculty[i].getdata(); // input data for ith faculty staff[i].getdata(); // input data for ith staff faculty[i]. putdata(); // display data of ith faculty staff[i].putdata(); // display data of ith staff Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 6.
    Concept of referencevariables  provides an alias (alternative name) for a previously defined variable.  Syntax: data_type & reference_name=variable_name; data_type & reference_name=variable_name;  Example- int x = 10; int & y = x; // y is reference variable for x;  Both variables refer to the same memory location. Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 7.
    Concept of referencevariables cout<<x; // print 10 cout<<y; // print 10 x= x+5; cout<<x; // print 15 cout<<x; // print 15 cout<<y; // print 15 y=0; cout<<x; // print 0 cout<<y; // print 0 Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 8.
    Application of referencevariables  In passing arguments to functions.  Example- void swap(int x, int y) //swap values of two variables { int t=x; x=y; y=t;} { int t=x; x=y; y=t;} int main() { int a=5, b=10; swap(a,b); // function call by value cout<<a<<b; // print 5 and 10 return 0; } Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 9.
    Application of referencevariables void swap(int &x, int &y) //swap values of two variables { int t=x; x=y; y=t;} int main() { { int a=5, b=10; swap(a,b); // function call by reference cout<<a<<b; // print 10 and 5 return 0; } Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 10.
    Dynamic memory allocation Memory allocated at run-time.  “new” operator for memory allocation  “delete” operator to free the allocated memory Syntax:  Syntax: pointer-variable = new datatype;  Example- int *p=new int; // allocate integer size memory Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 11.
    Dynamic memory allocation *p = 10; // assign value 10 to newly created memory  int *p=new int(10);// initialize the memory with value 10 10  new can be used to create a memory space for any data type including user defined such as arrays, structures, and classes.  int *p=new int[15];// creates memory for an array of 15 integer elements Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 12.
    Dynamic memory allocation For multi-dimensional arrays, all the array sizes must be supplied.  Example- int *q=new int[5][3][4];// legal int *q=new int[5][3][4];// legal int *q=new int[m][3][4];// first dimension may be variable, all other must be constant. int *q=new int[5][3][ ];// illegal int *q=new int[ ][3][4];// illegal Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 13.
    Dynamic memory allocation When allocated memory is no longer needed, it is destroyed to release the memory space for reuse.  Syntax- delete pointer-variable; delete pointer-variable;  Example- delete p; // free the memory pointed by pointer p. delete [ ] q;// free the entire array pointed by pointer q. Prepared by: Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.
  • 14.
    Thank You Prepared by:Anil Kumar Tailor, Assistant Prof., Engineering College Ajmer.