Class
Define Class
//Rectangle.h
#ifndef RECTANGLE_H
#define RECTANGLE_H
class Rectangle
{
private:
double width;
double length;
public:
//accessor
double getWidth() const;
double getLength() const;
double getArea() const;
//mutator
void setWidth(double w);
void setLength(double l);
};
#endif
//Rectangle.cpp
#include "Rectangle.h"
double Rectangle::getWidth() const
{
return width;
}
double Rectangle::getLength() const
{
return length;
}
double Rectangle::getArea() const
{
return width*length;
}
void Rectangle::setWidth(double w)
{
width = w;
}
void Rectangle::setLength(double l)
{
length = l;
}
//main.cpp
#include <iostream>
#include "Rectangle.h"
int main(int argc, char *argv[])
{
Rectangle r;
r.setWidth(5);
r.setLength(10);
std::cout<<"Area: "<<r.getArea()<<std::endl;
return 0;
}
- private, cannot be accessed outside the class
- public, can be accessed by code outside the class
- const after the parentheses in a member function specifies that the function will not change any data stored in the calling object, must appear in both the declaration and the function header
- accessor, member functions that get value from a class's member variable but not make any change
- mutator, member functions that stores a value in member variable or changes the value of member variable
Pointers to Objects
#include <iostream>
#include "Rectangle.h"
int main(int argc, char *argv[])
{
Rectangle *r;
r = new Rectangle();
r->setWidth(5);
r->setLength(10);
std::cout<<"Area: "<<r->getArea()<<std::endl;
delete r;
r = 0;
return 0;
}
- call member function by -> operator
- (*r).getWidth() equals to r->getWidth()
Object Array
#include <iostream>
#include "Rectangle.h"
int main(int argc, char *argv[])
{
const int size = 2;
Rectangle array[size] = {Rectangle(), Rectangle()};
for(int i = 0; i < size; i++)
{
array[i].setWidth(i+1);
array[i].setLength(2*(i+1));
}
for(int i = 0; i < size; i++)
std::cout<<"Area "<<i<<": "<<array[i].getArea()<<std::endl;
return 0;
}
Array of Pointers to Objects
#include <iostream>
#include "Rectangle.h"
int main(int argc, char *argv[])
{
const int size = 2;
Rectangle *array[size];
for(int i = 0; i < size; i++)
array[i] = new Rectangle();
for(int i = 0; i < size; i++)
{
array[i]->setWidth(i+1);
array[i]->setLength(2*(i+1));
}
for(int i = 0; i < size; i++)
std::cout<<"Area "<<i<<": "<<array[i]->getArea()<<std::endl;
for(int i = 0; i < size; i++)
{
delete array[i];
array[i] = 0;
}
return 0;
}
Class Template
#ifndef V_H
#define V_H
class V
{
private:
int size;
int *array;
public:
//constructor
V(int s):size(s)
{
std::cout<<"Constructor ..."<<std::endl;
array = new int[size];
for(int i = 0; i < size; i++)
array[i] = 10*i;
std::cout<<"----End Constructor ..."<<std::endl;
}
//copy constructor
V(const V &right):size(right.size), array(size?new int [size]:nullptr)
{
std::cout<<"Copy Constructor ..."<<std::endl;
std::copy(right.array, right.array+size, array);
std::cout<<"----End Copy Constructor ..."<<std::endl;
}
//move constructor
V(V && right):size(0), array(nullptr)
{
std::cout<<"Move Constructor ..."<<std::endl;
size = right.size;
array = right.array;
right.size = 0;
right.array = nullptr;
std::cout<<"----End Move Constructor ..."<<std::endl;
}
int *getAddress() const {return array;}
int getSize() const {return size;}
void display() const
{
for(int i = 0; i < size; i++)
std::cout<<array[i]<<" ";
std::cout<<std::endl;
}
V & time(int n)
{
for(int i = 0; i < size; i++)
array[i] *= n;
return *this;
}
//copy assignment
const V& operator=(const V &right)
{
std::cout<<"Copy Assignment ..."<<size<<std::endl;
if(this != &right)
{
V temp(right);
//std::swap(size, temp.size);
//std::swap(array, temp.array);
std::swap(*this, temp);
}
std::cout<<"----End Assignment ..."<<std::endl;
return *this;
}
//move assignment
V & operator=(V&& right)
{
std::cout<<"Move Assignment ..."<<std::endl;
if(this != &right)
{
delete [] array;
array = 0;
size = 0;
size = right.size;
array = right.array;
right.size = 0;
right.array = nullptr;
}
std::cout<<"----End Move Assignment ..."<<std::endl;
return *this;
}
~V()
{
std::cout<<"Destructor ..."<<size<<std::endl;
delete array;
array = 0;
size = 0;
}
};
#endif