Lecture 3
Lecture 3
Sandeep Kumar
Class:
● A class describes the data and the methods(functions) to manipulate data.
● Syntax:
● private
● public
● protected
● By default, functions and data declared within a class are private to that
class and may be accessed only by other members of the class.
● The public access specifier allows functions or data to be accessible to
other parts of your program.
● The protected access specifier is needed only when inheritance is involved.
● Notice the required semicolon after the class declaration/definition close
brace.
● point is a class.
● x, y, z, theta are data members.
● setX(float), setY(float), … are member
functions (methods).
● point() is the constructor.
E.g.
Creating object:
This will create an object s1 and will invoke above constructor and initialize
data of the object.
Data Hiding:
● private and protected data members can not be accessed outside the class.
You can have another constructor that also assigns the data members/variables of
the object.
If you want to define an object with specific values of data members one can use
above constructor.
● Constructor has the same name as the class. They do not have any return type.
Assignment and Conversion:
point p,
p = point(3, 4, 8, 60)
point(3, 4, 8, 60), invokes the constructor and created an unnamed class object that
is assigned to the object p.
point p, q;
q = point(4, 5, 7, 60)
p = q;
● Here this is a pointer to the calling object. (*this) gives the content of the calling object.
● this is the address of the calling object and the dereferencing operator *, access the
content of the object (of address);
●
Pointer:
#include<iostream>
using namespace std;
int main(){
int *p;
int x;
x = 5;
cout<<"x = "<<x<<endl;
p = &x;
cout<<"p = "<<p<<endl;
cout<<"Value at p = "<<*p<<endl;
*p = *p + 1;
cout<<"Updated value of x = "<<x<<endl;
}
cout << P