C++ Classes
- The class is the capsule that is used to encapsulate an abstract
data type.
- It controls access to the data that is in the ADT and also defines the
operations that can be performed on an object of this type.
- It provides support for the object oriented programming ideas of: class,
polymorphism, inheritance. Class Support for OOP
- A class defines a new data type. You can create many objects of
this type.
- Functions and operators can be overloaded. Names defined in a
class are scoped only for that class. Overloading can occur within a
class based on call sequence.
- A class can be used as the base class for a derived new class.
The derived class inherits all of the properties of the base class.
The derived class can add new members or change base class members.
C++ Support for OOP (cont.)
- In a C++ program the OOP paradigm is centered around your class
definitions.
- You can not spend too much time thinking about the structure of your data
and designing your classes BEFORE writing a line of code.
- Reread the previous bullet item! Syntax of a Class Definition
- Syntactically it looks a lot like a C struct or Pascal record.
- A class is composed of one or more members.
- Members are:
- data items (members)
- functions (member functions)
- Class definition usually placed in an include (.h) file for ease
of use.
A Complex Number Class
class Complex
{
private:
double re;
double im;
public:
Complex(float r,float i) {re = r; im = i;}
Complex(float r) {re = r;im = 0.0;}
~Complex() {};
double Magnitude(); // calculate magnitude
double Real() {return re;} // return real part
inline double Imag(); // return imaginary part
Complex operator+(Complex b)
{return Complex(re + b.re,im + b.im);}
Complex operator=(Complex b)
{re = b.re;im = b.im; return *this;}
};
inline double Complex::Imag()
{
return im;
}
double Complex::Magnitude()
{
return sqrt(re*re + Imag()*Imag());
}
Access Control
- Unless specified otherwise members of a class are accessible only
from code within the class.
- private - the default condition but can be explicitely specified
in class definition also
- public - provides access to member or member function by code
outside of the class
- protected - allows access from code in a derived class
- friend - provides selective privileged access to private members
- Typically, data members are all private. Provide public access
to them via public member functions. Member Functions
- Define the operations that can be performed with class objects.
- Must define the return type and parameter sequence.
- Functions can be inlined for effeciency.
- Special member functions called constructors and destructors.
Defaults provided by compiler.
- Operator member functions.
- Member access using member name only.
- this is pointer to current object.
Constructors
- Called whenever a new object of the class is created.
- Can be created implicitely as automatic variable and function
parameter or explicitely with the new operator.
- new returns a pointer to the new object.
- Constructors can be overloaded.
- Must initialize all data members.
- Care when using pointers to other data objects as data members
Destructors
- Called whenever an object is destroyed.
- Done implicitely at the end of the lifetime of an automatic
variable and value function parameter or explicitely with the delete
operator.
- Can not be overloaded.
- Do not leave memory leaks!
Usage
#include
#include "Complex.h"
main()
{
Complex a(1.0,1.0);
Complex b(5.0);
printf("a real = %f a imaginary = %f\n",a.Real(),
a.Imag());
printf("b real = %f b imaginary = %f\n",b.Real(),
b.Imag());
}
#include
#include "Complex.h"
main()
{
Complex *a;
Complex *b = new Complex(5.0);
Complex c(0,0);
a = new Complex(1.0,1.0);
c = a + b;
printf("a real = %f a imaginary = %f\n",a->Real(),
a->Imag());
printf("c real = %f c imaginary = %f\n",c.Real(),
c.Imag());
delete a;
delete b;
}
Operator Functions
Casting Functions
- operator type(), where type is the data type to convert object to
- explicitly called as (type)x which casts object x as a type
- compiler will implicitly do it using the constructor if available
a = b + 2
, where a
and b
are
complex objects, is translated to
a = operator+(b,Complex(double(2)))
Derived Class
- Allows you to build new classes making use of previously defined ones
- Use it to expand or modify the operation of a class
- Derived class inherits all of the base classes members
- Derived class has access to public and protected members
- Base class constructor is called first. Then member
constructors. Then derived class constructor. Opposite order for
destructors.
Derived Class Example
class Employee
{
char* name;
int age;
int department;
int salary;
public:
Employee(char* name);
void print();
};
class Manager : public Employee
{
EmployeeList employees;
public:
Manager(char* name, Employee* people);
void print();
};
Derived Class Operation
- public base makes all public members of base class public in
derived class
- can also list specific public base members to be public in
derived class:
class Manager : Employee
{
// ...
public:
Employee::print();
};
- Parameters for base class and member constructors provided in
definition of derived class constructors
Manager::Manager(char* name,Employee* people) :
(name),employees(people)
{
// ...
}
- Access to redefined public base class function
void Manager::print()
{
// print manager as an employee
Employee::print();
// ...
// print manager's manager info
}
- Pointer to a derived class can be stored in a public base class
pointer without any conversion needed.
Employee* ePtr;
Manager m(...);
ePtr = &m;
Deriving Multiple Classes
Virtual Functions
- Given a pointer to a base class object to which derived type
might it belong?
Employee* ePtr;
Secretary s;
ePtr = &s;
ePtr->print();
- Base class can declare virtual functions that are redefined in
derived classes and the compiler is responsible for sorting it out.
class Employee
{
// ...
public:
virtual void print();
};
- Base case must define a function for the virtual function unless
it is an abstract class. This function is the default function.
- Derived class can define its own function to override the base
class function.
class Manager : public Employee
{
// ...
public:
void print();
};
class Secretary : public Employee
{
// ...
public:
// no public print()
};
main()
{
Manager m(...);
Secretary s(...);
// uses print() in Manager derived class
m.print();
// uses print() in base Employee class
s.print();
}