You are on page 1of 12

LECTURE 4-1

TASTE OF C++
March 29 2016
Outline
• The components of “class” in C++
• Variables and Methods
• Creator and Destructor of a Class
• Inheritance
Class
• The class in C++ is very similar to, but more extended from the structure in C.
• Any class has variables and methods as its members.
• There are access specifier for the members.

class Quadrangle
{
private: Access specifier
int width;
int height; Members
int area;
public : Access specifier
int CalcuateArea ( int argWidth, int argHeight)
{
width = argWidth; Method
height = argHeight;
area = width * height;
return area;
};
};
Example of using Class
#include <iostream>

class Fish
{
public:
void Swim()
{
printf("You invoke swim()\n");
};
};

int main()
{
Fish fish;
fish.Swim();
}
‘.’ pperator is used in the same way as in the structure of C-language
Access Specifier
• private members of a class are accessible only by other members (methods) of the
same class
• protected members are accessible from other members of the same class, but also
from members of their derived (inheriting) classes.
• public members are accessible from anywhere where the object is visible.
Example
class Quadrangle
{ int main()
private: {
int width; Quadrangle A;
int height; A.width=3; // Error
int area; A.CalcuateArea(3, 2); // O.K
}
public:
int CalcuateArea ( int argWidth, int argHeight);
};

int Quadrangle::CalcuateArea ( int argWidth, int argHeight)


{
width = argWidth;
height = argHeight;
area = width * height;
return area;
};
Creator and Destructor of a Class
• A creator of a class is automatically called whenever an object is declared.
• By explicitly defining a ‘constructor’ method, an object can be initialized.
• Whenever the routine reaches the end of any function, the destructor of all the
class variables are automatically called, to free the memories assigned to them.
* Important: user cannot arbitrarily call the destructor. If he/she does it by
calling ~Unit() in the example below, it simply executes the commands inside
~Unit() function. It does not actually free the memory.
• Constructor and destructor should take the same name as the class. In front of
the destructor, use ~ sign.
#include<iostream>
Unit::~Unit()
class Unit { {
printf("Unit destructing...\n”);
int status; status = 0;
}
public:
Unit(); void Unit::fly()
~Unit(); {
void fly(); printf(“%d\n”,status);
}; }

Unit::Unit() int main(int argc, char* argv[])


{ {
Unit oUnit;
printf( “Unit constructor \n”); oUnit.fly();
status = 1; return 0;
} }
Inheritance
• In object-oriented programming (OOP), inheritance means that an object or class is
based on another object or class.
• It is a mechanism for code reuse.

class NPC { class Grunt : public NPC {


int Armor;
int defense; public:
void SetArmor(int n);
public: int GetArmoredDefense();
void SetDefense(int n); };

int GetDefense();
};
Parent class Child class
Sample float TriangleArea::Calculate()
{
#include <iostream> float area = 0 ;
class TriangleArea area = width * height;
{ area = area / 2.0;
private:
int width;
int height; return area;
}
public:
TriangleArea(int argWidth, int argHeight); void main()
float Calculate(); {
}; int width = 0, height = 0;

TriangleArea::TriangleArea(int argWidth, int argHeight) cout << ”Width= ”; cin >> width;
{ cout << ”height= ”; cin >> height;
width = argWidth;
height = argHeight;
} TriangleArea* triArea =
new TriangleArea(width, height);
cout << ”The ares is " <<
triArea->Calculate() <<
“square centimeter”<< endl;
}
Exercise
Exercise 1
Make C++ program, which prints out “Hello World”. The printf function should be
called as a ‘method’ of a class.
Homework
Question 1
Make a C++ program, which gets two float numbers from the command line, and
calculate the product of them.

Question 2
Extend your code using ‘inheritance’, from which a sum of the two numbers is
calculated.

Due on 24:00 April 5, 2016


END OF
LECTURE 4-1

You might also like