INTRODUCTION : INTRODUCTION C++ as a Object Oriented Programming Language.
Goals : Goals A Brief about Object Oriented Programming using C plus plus.
Know about Classes and Objects
Construction and Destruction of Objects
Calling Member Functions.
Classes : Classes A class is an expanded concept of a data structure: instead of holding only data, it can hold both data and functions.An object is an instantiation of a class. In terms of variables, a class would be the type, and an object would be the variable.Classes are generally declared using the keyword class, with the following format:class class_name { access_specifier_1: member1; access_specifier_2: member2; ... } object_names;
Slide 4 : class Crectangle
{ int x, y;
public:
void set_values (int,int);
int area (void); } rect;
rect.set_values (3,4); myarea = rect.area();
Slide 5 : Here is the complete example of class CRectangle:// classes example
#include
using namespace std;
class CRectangle {
int x, y;
public:
void set_values (int,int);
int area () {return (x*y);} };
void CRectangle::set_values (int a, int b) { x = a; y = b; } int main () { CRectangle rect; rect.set_values (3,4); cout << "area: " << rect.area(); return 0; } area: 12
The most important new thing in this code is the operator of scope (::, two colons) included in the definition of set_values(). It is used to define a member of a class from outside the class definition itself.
Constructors : Constructors CRectangle rect (3,4); CRectangle rectb (5,6);
Constructors cannot be called explicitly as if they were regular member functions. They are only executed when a new object of that class is created.
Destructors : Destructors The destructor fulfills the opposite functionality. It is automatically called when an object is destroyed.
Conclusion : Conclusion A class can have data, functions to access this data and how data can be accessed.
We learnt how classes and objects are defined and data objects can be constructed as well as memory released.