Computer Science, asked by vn2727197, 11 months ago

Define array within a class and array of object .give some example ?

Answers

Answered by sivasujithkumar
2

Answer:

1. Array within a Class AS WE HAVE ARRAYS WITHIN A STRUCTURE. IN THE SAME WE CAN HAVE ARRAYS AS A DATA MEMBER WITHIN A CLASS ALSO. THEY CAN BE DECLARED EITHER IN PRIVATE OR PROTECTED OR PUBLIC SECTION.

2. Example:- Array as data member of Class  Class student  {  Int rollno;  Char name[50];  Int marks[5]; // arrary of 5 intgers variables to store marks in 5 subjects.  Int total;  Float average;  Public:  Void getstuddetails();  Void printstuddetails(); }; Void student:: getstuddetails() { cout<<“Enter rollno”; cin>>rollno; cout<<“Enter Name”; gets(name); total=0; cout<<“Enter marks in 5 sujects”; for(int i=0;i<5;i++) { cout<<“Subjet “<< i+1<<“t”; cin>>marks[i]; total=total+marks[i]; } }

3.  Void student :: printstuddetails()  {  Cout<<“Rollno t:t”<<rollno;  Cout<<“Name t:t”<<namel  Cout<<“Marks Details”;  For(int i=0;i<5;i++)  {  Cout<<“Subject “<<i+1<<“t:t”<<marks[i];  }  Cout<<“total t:t “<<total;  Cout<<“Average t :t “<<average;  }

4. Array of Objects  As we can declare array of integer, float, characters and array of structure variables in the same way C++ also supports Array of objects also.  An array of objects is declared in the same way as we had declared array of structure variables.

5. Example:- In continuation with the previous example of array within a class  Void main()  {  Student xa[10];  Cout<<“Enter detail of 10 students of x a class”;  for(int i=;i<10;i++)  {  Xa[i].getstuddetails();  }  Cout<<“Details entered by you are shown below”;  For(i=0;i<10;i++)  {  Xa[i].printstuddetails();  }  }

6. Nested Class As of nested condition (conditions within conditions) and nested loops (loops within loop). We can also declare Nested class.  A class declared within another class is called a nested class.  The outer class is known as enclosing class.  The inner class is known as nested class.  We can declare the inner class (nested class) in any section of the outer class (enclosing class) i.e. private , public or protected.  If we declare inner class in private section then we will be able to declare the objects of inner class within outer class only or you can’t declare object of inner class (declared in private section) Outside the outer class(its enclosing class).

7.  If the nested class definition in under public section the object of inner class can be declared outside the outer class but by specifying full qualified names. Only. Example: Outerclassname :: innerclassname objname;

8. Example 1:- Nested class declared in private section

9. Functions in a Class Different type of functions can be declared within a Class.  Inline function  Constant functions  Nested functions

10. Inline functions  It is an enhancement of C++ to speed up the execution of a program.  Coding of inline function is same as normal function but the definition of inline function starts / precedes with the keyword inline.  Difference between normal and inline function is the different compilation process of them.

11. Execution process of a normal function  1001 int a=4, b=7; Memory loaded code scenario  1002 int c= a+b, d; with memory addresses of  1003 d=square ( c); instruction to be executed.  1004 cout<<d;  2011int square(int i)  {  Return i*I;  }

12. Whenever in the execution of a program a function call takes place a lot of function calling overheads involved.  The address of the next instruction ( nest to the function calling instruction) is saved in the memory. i.e. 1004 address will be saved in the memory  The argument passed to function will be copied into system stack (local variable storage area) i.e. value of c (11) will be loaded into stack.  Jump to the memory address of called function i.e. jump to 2011 address

13.  Execute the function i.e. calculate 11 X 11 and return the result  Store the return value into d.  Fetch the address saved in step 1 and jump to that address (1004) start further execution.

Explanation:

Similar questions