Abhiram Ranade
Topics
l
Classes
l
contd
struct Complex{
private:
double r, i;
public:
real_part(){ return r;}
imaginary_part{ return i;}
Complex(double r1, double i1){r=r1; i=i1;}
}
l
Simplecpp graphics
l
Specification
l
Constructor:
Button(double cx, double cy, double w,
double h, char * textptr);
l
Member function:
bool isInside(double clickx, double clicky);
Example of use
initCanvas();
Button b(100,100,100,50,Forward);
Turtle t;
while(true){
int cp = getClick();
if(b.isInside(cp/65536, cp%65536)) t.forward(5);
else if ...
}
Button class
class Button{
Rectangle r; Text t;
double cx, cy, width, height;
public:
Button(double x, double y, double w,
double h, char * textptr);
bool isInside(double clickx, double clicky);
} // member function bodies can be given //
outside
Constructor Body
Button::Button(double x, double y, double w,
double h, char *tptr){
cx = x;
cy = y;
width = w;
height = h;
r.reset(cx,cy,w,h);
t.reset(cx,cy,tptr);
}
Note
l
l
l
isInside
bool Button::isInside(int clx, int cly){
return (abs(cx - clx) <= width/2)
&& (abs(cy - cly)<= height/2);
}
Destructor Functions
l
l
l
A remark
l
l
l
l
Inefficient in general
#include <string>
l
int main(){
string p=abc, q="defg";
string r=p+q; // concatenation
cout << p << "," << q << "," << r.length() <<endl;
p[2] = '1'; q[0] = p[0];
string t; getline(cin,t); // read line from keyboard
cout <<q.substr(2); // starting at index 2, to end.
string s = q.substr(1,2) // starting at 1, length 2.
int i = r.find("ab");
int j = r.find("ab",1);
Exercise
l
Solution idea
l
l
getline(cin, line);
Details
l
Example
int main(){
int *ptr, *ptr2;
ptr = new int;
*ptr = 35;
ptr2 = new int[10];
for(int i=0; i<10; i++) ptr2[i] = i*i;
cout << *ptr <<' '<< ptr2[5] << endl;
delete ptr; delete[] ptr2;
}
Print variables.
Concatenate strings.
b = "stu";
String c = a + b;
b = a;
a.print(); b.print(); c.print();
String d[2]; d[0] = c + a;
d[0].print(); d[1].print();
}
d[1] = "xyz";
How to implement
l
l
l
Constructor
String(){ ptr = NULL; }
NULL is abbreviation for 0.
NULL is used to indicated that the pointer
does not point to anything meaningful
Use NULL instead of 0 for readability.
Assignment a = abc;
C++ considers this to be the same as a.f(rhs)
where f is a function operator=, and rhs is
abc. abc has type char*.
So all we need to do to support this is to
define member function with declaration:
void operator=(char *rhs);
Assignment a = abc;
void operator=(char *rhs){
ptr = new char [length(rhs)+1];
strcpy(ptr, rhs);
}
What if we write:
String a; a = abc; a = defg;
Assignment a = abc;
void operator=(char *rhs){
delete[] ptr; // OK even if ptr = NULL
ptr = new char [length(rhs)+1];
strcpy(ptr, rhs);
}
Assignment a = b;
void operator=(String rhs){
delete[] ptr; // OK even if ptr = NULL
ptr = new char [length(rhs.ptr)+1];
strcpy(ptr, rhs.ptr);
}
Many operator= functions can be defined,
provided they take parameters of different
types.
Concatenation: a + b;
Note that C++ considers a + b to be a.f(b)
where f is the function operator+
So we just need to define member function
operator+
This will return a String. So its declaration is:
String operator+(String b);
Concatenation a + b
String operator+(String b){
String res;
res.ptr = new char[length(ptr) + length(b.ptr)];
strcpy(res.ptr, ptr);
strcpy(res.ptr, b.ptr, length(ptr));
return res;
} // strcpy(x, y, m): copies into x[m], x[m+1]
Creating vectors
vector<int> v1; //zero length vector, int elements.
vector<char> v2(10); //length 10, char elements.
vector<int>v3(5,91); // length 5. each = 91.
vector<int>v4(v3); // copy of v3.
Sorting vectors
Need #include <algorithm>
vector<int> v(10);
for(int i=0; i<10; i++) cin >> v[i];
sort(v.begin(), v.end()); // sorts v!
v.begin(), v.end() : iterators. Say which
portion of v to sort. Discussed later.
Remarks
cin >> next : read value into next. But this is
an expression also.
Value of expression: cin if reading succeeds. 0
if reading fails i.e. False.
Reading can fail if file ends or any other
error, e.g. non-numeric input.
To signal end of file from keyboard, type ^d.
Main program
int main(){
vector<Student> svec;
Student s;
while(cin >> s.rollno){
cin >> s.marks; svec.push_back(s); }
sort(svec.begin(), svec.end());
for(int i=0; i<svec.size(); i++) cout
}