Lecture 3 DS
Lecture 3 DS
and Algorithm
21COMP04C
2023
Lecture3
1
Lab2 submission
• Labs are ungraded
• Lab 2 submission is next Sunday (
2
References
• The “pointer” and “reference” both are used to point or refer an
another variable
pointers reference
int a = 5,b=6; int a = 5,b=6; A reference must be initialized
int *p = &a; int &ref = a; when it is declared.
int *p = &a; int &ref = a; reference can’t be reassigned
p=&b &ref = b; //invalid
A pointer can be assigned to a References cannot be NULL NULL
NULL value.
int a = 5; int a=5; Reference are easy to use
int *p = &a; int &ref = a;
*p=6 ref=6;
cout<<a; //print 6 cout<<a //print 6
• delete [] first;
4
2-Deep copy
• Two or more pointers have their own data
• Manually create a new array and copy all elements
int *first= new int[10]
………..
int *second;
//Deep copy operation
second = new int[10];
for (int i=0;i<10;i++)
{
second[i]=first[i] FIGURE 3-19 first and second both pointing to their own data
}
delete [] first;
5
Lecture 3
Inheritance
Polymorphism
Lecture Resources
Data-Structure Using C++ by D.S. Malik (ch 1)
Data Structures Using C by Reema Thareja (ch2)
These slides are adapted for the text book's slides 6
Passing parameters of a functions by value (Copy)
int sum_and_inc (int x, int y)
{
x++; y++; return x+y;
}
Values of a and b and
void main()
{
copied to x and y , the
int a=3, b= 3,c; same can happened to class
c=sum_and_inc(a,b) object
cout<< a<<b<<c<<endl
}
Output
338
Passing parameters to a function by pointers
int sum_and_inc (int *x, int *y)
{
(*x)++; (*y)++; return *x+*y;
}
int main()
{
int a=3, b= 3,c;
c=sum_and_inc(&a,&b);
cout<< a<<b<<c<<endl
}
Output
448
Passing parameters to a function by pointers
int sum_and_inc (int &x, int &y)
{
x++; y++; return x+y;
} Also use reference when passing
void main() objects because copying objects is
{ expensive
int a=3, b= 3,c;
c=sum_and_inc(a,b); The general rule would be:
cout<< a<<b<<c<<endl If you are just going to read the object, pass a const reference
If you are going to modify the object, pass a reference (or pointer).
} If you are definitely going to make a copy of the object, pass it by
value.
Output
448 void func ( const clocktye &myclock)
Inheritance
• Inheritance lets us create new classes from existing classes.
• The existing classes are called the base classes;
• The new class that we create from the existing classes is called the
derived class.
• The derived class inherits the properties of the base classes
• Inheritance can reduce software complexity.
Example
Person
Source:://patilinfotech.wordpress.com/2018/03/09/inheritance/
Defined single inheritance Shape
base
Circle
Derived
Example:
Public single inheritance
Source: https://www.bogotobogo.com/cplusplus/private_inheritance.php
Friend Functions of Classes
• A friend function of a class is a nonmember function of the class, but
has access to all the members (public or non-public) of the class
class class_friend
{
friend void Func(parameter 1, parameter2,…….
.
. Where to put friend
}; No friend in definition
void Func(/*parameters*/)
{ ….. }
void friendFunc(Friendclass &Object1) class Friendclass
{ {
Object1.x = 88;//Error if not friend friend void friendFunc(Friendclass object1);
Object1.print(); public:
// local variable void print();
Friendclass Object2; void setx(int a);
Object2.x = 45; private:
Object2.print(); } int x; };
The operators
. .* :: ?: sizeof //may not be overloaded.
All other operators may be overloaded, i.e.
+ - * / = < > += -= *= /= << >> <<= >>= == != <= >= ++ -- % & ^ ! | ~ &= ^= |=
&& || %= [] () , ->* -> new delete.