0% found this document useful (0 votes)
14 views19 pages

Lecture 3

The document discusses C++ classes and some key concepts like class definition syntax, access specifiers, constructors, operator overloading, and pointers. It provides examples of defining a Point class with data members like x, y and member functions to set and get values. It also demonstrates overloading operators like ==, != and << to allow comparing and printing Point objects.

Uploaded by

ashodhiya14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views19 pages

Lecture 3

The document discusses C++ classes and some key concepts like class definition syntax, access specifiers, constructors, operator overloading, and pointers. It provides examples of defining a Point class with data members like x, y and member functions to set and get values. It also demonstrates overloading operators like ==, != and << to allow comparing and printing Point objects.

Uploaded by

ashodhiya14
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

C++ for Mathematics

Sandeep Kumar
Class:
● A class describes the data and the methods(functions) to manipulate data.
● Syntax:

Here object-list is optional. Access specifier can be of three types:

● 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.

A constructor is called when a object


of the class is created.

When we created the object s (in main


program), the constructor is invoked
and the value of x, y, x, theta are set
to zero.

Note cout<<s.getX(), prints 0.


● There can be other constructors other than default constructor.

E.g.

point(float x, float y, float r, float a){

x = x1; y = y1; r = r1; theta = a; }

Creating object:

point s1(3, 4, 5, 60);

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.

● Public data/methods can be accessed outside the class.

● If you want data of a class to be handled/manipulated in a specific way and


want to keep it safe from outside world, declare the variables as
private/protected.
● The functions./methods that process/manipulate the data are defined as public.
Constructors:
● A constructor is a method that is invoked when an object of a class is declared.
● For example, the following code
point s;
On declaring an object the default constructor is invoked.

You can have another constructor that also assigns the data members/variables of
the object.

E. g point(float x1, float y1, float r1, float a)

If you want to define an object with specific values of data members one can use
above constructor.

point s1(3. 4. 5. 60);

● 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)

Will create a class object p with data variables value as 3 and 4.

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;

will copy data members of q to data members of p.

That is it will execute p.x = q.x, p.y = q.y, ….


Point.h
● Including the Point.h filed in yout C++ program is equivalent to including the
Point class declaration in your progam.
● So the Class and its data members/ methods are already declared. We just need
to define these.
● A class method can be defined outside the class as follows:

class_name:: method_name(input arguments){ }


#include "Point.h"
Point::Point(){
x = 0;
y = 0;
}
Point::Point(double xx, double yy ){
x = xx;
y = yy;
}
double Point::getX() const {
return x;
}
double Point::getY() const {
return y;
}
int main(){
Point s;
cout<<"x component of point = "<<s.getX()<<endl;
return 0;
}
● The const specifiers tells that the function will not change/update data
members.
● The void type means that the method do not return anything.

Operator Overloading:
#include<iostream>
using namespace std;
int main(){
cout<<"== operator on int "<<(5 == 10)<<endl;
cout<<"== operator on float "<<(10.0 == 10.0)<<endl;
return 0;
}

Similarly + operator world for int, float, complex, ….

The different behaviour of an operator depending on operands is called operator


overloading.

We want to ==, so that it can compare two Point type variables.


bool operator==(const Point& Q) const;

● We expect == to return True or False , a bool variable .


● operator is the keyword that that declares that we want to overload the ==
operator.
● The above operator is member method of the Point class. So it will be invoked
through an object of Point class.
● Here const means the object will not be changed. The & signifies that the
reference of Q is passed.
● The last const, specify that it will not change the value of the object.
}
Another definition of != operator

bool Point::operator!=(const Point &Q) const {


return ! ( (*this) == 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

● The operator << is another example of operator overloading.


● It tools two inputs the cout (osstream, output stream from left) and data P
from right.
● The result of above statements is also an output stream, that prints on the
screen.
● We want to overload the operator <<, so that cout<<P, prints the x and y
components of the point P.
That is we want cout<<P to output: (P.getX(), P.getY())
ostream& operator<<(ostream& os, const Point& P);

● The output type is a reference to osstream (output stream).


● The name of the function is operator<<.
● It takes two inputs an osstream and a Point P.
● cout<<P, is actually same as calling operator<<(cout, P).

ostream& operator<<(ostream& os, const Point& P) {


os << "(" << P.getX() << "," << P.getY() << ")";
return os;
}

You might also like