Escolar Documentos
Profissional Documentos
Cultura Documentos
• A constructor has:
• You must supply the arguments to the constructor when a new instance is
created.
Example
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
public:
};
rectangle::rectangle(float h, float w) {
height = h;
width = w;
xpos = 0;
ypos = 0;
}
Example
void main() {
rc.posn(100, 100);
rc.draw();
rc.move(50, 50);
rc.draw();
Overloading constructors
• You can have more than one constructor in a class, as long as each has a
different list of arguments.
• Example
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
public:
};
rectangle::rectangle() {
height = 10;
width = 10;
xpos = 0;
ypos = 0;
void main() {
rectangle rc2();
rc1.draw();
rc2.draw();
• Example
class properties {
private:
int color;
int line;
public:
};
properties::properties(int c, int l) {
color = c;
line = l;
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
public:
};
height = h;
width = w;
xpos = 0;
ypos = 0;
};
void main() {
What is a destructor?
A destructor has:
(i) the same name as the class but is preceded by a tilde (~)
(ii) no arguments and return no values
Example
class string {
private:
char *s;
int size;
public:
~string(); // destructor
};
string::string(char *c) {
size = strlen(c);
s = new char[size+1];
strcpy(s,c);
string::~string() {
delete []s;
Destructors
Example
class rectangle {
private:
float height;
float width;
int xpos;
int ypos;
public:
};
height = old_rc.height;
width = old_rc.width;
xpos = old_rc.xpos;
ypos = old_rc.ypos;
void main() {
// copy constructor
}
Defining copy constructors is very important
• Default copy constructors work fine unless the class contains pointer data
members ... why???
• Example
#include <iostream.h>
#include <string.h>
class string {
private:
char *s;
int size;
public:
~string(); // destructor
void print();
};
void string::print() {
strcpy(s, c);
void main() {
string str1("George");
str2.print();
str2.copy("Mary");
str2.print();
Operator Overloading
class OverloadingExample {
private:
int m_LocalInt;
public:
m_LocalInt = j;
};
void main() {
OverloadingExample object1(10);
}
Types of Operator
Unary operator
Binary operator
Unary Operators
Operators attached to a single operand (-a, +a, --a, a--, ++a, a++)
class UnaryExample {
private:
int m_LocalInt;
public:
UnaryExample(int j) {
m_LocalInt = j;
int operator++ () {
return (m_LocalInt++);
};
void main() {
UnaryExample object1(10);
Non-Overloadable Operators
Automatic type conversion by the C++ compiler from the type that
doesn’t fit, to the type it wants
Constructor conversion
Operator conversion
class One {
public:
One() {}
};
class Two {
public:
Two(const One&) {}
};
void f(Two) {}
void main() {
One one;
}
Automatic Type Conversion
Automatic type conversion by the C++ compiler from the type that
doesn’t fit, to the type it wants
Constructor conversion
Operator conversion
Constructor Conversion
class One {
public:
One() {}
};
class Two {
public:
Two(const One&) {}
};
void f(Two) {}
void main() {
One one;
Operator Conversion
class Three {
int m_Data;
public:
};
class Four {
int m_Data;
public:
Four(int x) : m_Data(x) {}
return Three(m_Data);
};
void g(Three) {}
void main()
Four four(1);
g(four);
You explicitly initialize a class object when you create that object.
The compiler calls the constructor of the class using this list as the constructor's
argument list.
The type of the single argument must match the type of the first argument to the
constructor.
If the constructor has remaining arguments, these arguments must have default
values.
The following example shows the declaration and use of several constructors
that explicitly initialize class objects:
// by constructor.
#include <iostream>
class complx {
public:
// default constructor
complx() : re(0), im(0) { }
// copy constructor
void display() {
cout << "re = "<< re << " im = " << im << endl;
};
int main() {
complx one(1);
// construct complx(3,4)
complx four;
complx five = 5;
one.display();
two.display();
three.display();
four.display();
five.display();
re = 1 im = 0
re = 1 im = 0
re = 3 im = 4
re = 0 im = 0
re = 5 im = 0