Oct 29 2022 Flashcards
(31 cards)
what is the syntax for return by address
Int * fun(int n) {
int *p=new int[n];
for(int i=0;i<n;i++)
p[i]=i+1;
return p;
}
what is the syntax for return by reference
Int & fun(int &a) {
cout«a;
return a; }
Explain Code
Int & fun(int &a) {
cout«a;
return a; }
Int main() {
int x=10; fun(x)=25; cout«x;
}
What does all of these mean
Return by Reference
* A function cal return reference
* It should not return reference of its local variables
* It can return formal parameters if they are reference
Once you understand these see what was the factor that made you understand this
What does all of these mean
Return by Reference
* A function cal return reference
* It should not return reference of its local variables
* It can return formal parameters if they are reference
Once you understand these see what was the factor that made you understand this
What are the main points regarding local variables vs global Variables
Local Variable
1) Local Variables are only accessible in that function ( even main)
2) Once the function terminates the they are removed from memory
Vs
Global Variable
1) Accessible by all functions
2) They are in the memory as long as the program is running
Where are the global variables stored
The code Section , created at loading time before the execution of the program
Where are local variables stored
The Stack
What is difference and similarity b/w between Static. and global variable
Similarity
-> They are both there till the end of program
They are created in code section
Difference
-> Anybody function can access global variable , only function where the static variable was declared can access the static variable
What is Modular Programming
Mostly just Functions that are relavent
How to avoid mishadling of data
Make data private show functions
What is Abstraction
How do we have POLYMORPHISM
Through Inheritance
data = ?
function = ?
property
behavior
- Many object can be created from same class
- Object consumes memory equal to sum of sizes of all data members * Member functions don’t occupy memory
- Member functions are called depending on object
- . Dot operator is used for accessing members of object
- Memory allocated for object is also called as instance
DO you understand these
If you want to access object property used name vs pointer write down the difference in syntax
r.length
p-> length ( Provided p stores the address of that object)
Syntax for creating object in Stack using pointer
VS
Syntax for creating object in Heap using pointer
Rectangle r;
Rectangle *p;
p = &r
vs
Rectangle *q = new Rectangle
What are the principles of Data Hiding
Data should be made private , function should be made public
What are the type of constructors
Default C
Non Parameterized C
Parameterized C
Copy C
What is the syntax of copy constructors and why
Test (Test &t){
a=t.a
p = new int[a]
}
The only thing is you are passing the object as reference , the name should be the same as class
Possible error with copy constructors
If you are using pointer it might accidentally lead the same address of the array
What are type of Functions a class can have
- Constructors - called when object is created
- Accessors / Getters - used for knowing the value of data members
- Mutators /Setters - used for changing value of data member
- Facilitator - actual functions of class
- Enquiry - used for checking if an object satisfies some condition * Destructor - used for releasing resources used by object
What is Scope Resolution Operators
What is the rule for Inline functions , and why do we have to do this
Never write complicated functions inside the class, the machine code for the function is written with the machine code of main function