Tutorial (Array and Structure)
Tutorial (Array and Structure)
#include <iostream>
int main()
{
int Array[] = {1,2,3,4,5}; // Declaring and Initializing an Array
return 0;
}
Now we will just change the arguments of the above function a little bit to
see another method of passing Array to function.
#include <iostream>
int main()
{
int Array[] = {1,2,3,4,5}; // Declaring and Initializing an Array
return 0;
}
return Arr;
}
int main()
{
int Array[] = {1,2,3,4,5}; // Declaring and Initializing an Array
return 0;
}
In this simple way we can return any type of array from any function.
Structure
Structure is Abstract data type or User defined data type. User make this
data type according to his requirement. Here we will see how we can
perform functions on structures. Is this look so simple? Yeah it is but most
of the students get confused in passing structure to function and
returning structure from a function.
#include <iostream>
#include <string>
string name;
string fatherName;
int age; // 3 members of structure
};
int main()
{
Student Jawwad;
printStructure(Jawwad);
return 0;
}
In this way we can pass A Structure to a function which will then perform
some specific task. We have a little problem here. As we know we are
passing structure by value so copy constructor will also called by
compiler here, also if we change anything in this function it will not effect
original values. How we can get rid of this?
#include <iostream>
#include <string>
string name;
string fatherName;
Tutorial | Array and Structure
Tutorial 6
int main()
{
Student Jawwad;
printStructure(Jawwad);
return 0;
}
#include <iostream>
#include <string>
string name;
string fatherName;
int age; // 3 members of structure
};
int main()
{
Student Jawwad;
return 0;
}
string name;
string fatherName;
int age; // 3 members of structure
};
cin>>stu.fatherName;
cin>>stu.age;
int main()
{
Student student;
printStructure(student);
printStructure(copystudent);
return 0;
}
In this way we can return an object by value. Now see how can we return
an object by reference.
#include <iostream>
#include <string>
string name;
string fatherName;
int age; // 3 members of structure
};
int main()
{
Student student;
printStructure(student);
printStructure(copystudent);
return 0;
}