05-Overview of C++ (Classes)
05-Overview of C++ (Classes)
• Class
• Object
• Constructor
• Destructor
class ClassName
{
public:
Declarations of public members
private:
Declarations of private members
};
class Rectangle {
private:
int width, height;
public:
void setSize (int, int) { … }
int area (void) { … }
};
CT077-3-2-DSTR Data Structures 8
Rectangle Class Example
Declares a class called
// classes example Rectangle
#include <iostream>
using namespace std;
Everything inside this opening
class Rectangle { brace and the corresponding
int width, height; closing brace is part of the class
public:
void setSize (int,int); Two data members (width and
int area () { height) with private access
return (width*height); (because private is the default
} access level)
};
void main () {
Rectangle rect;
rect.setSize(3,4);
cout << "area: " << rect.area();
We can also create the object in
Rectangle * r = new Rectangle; heap memory, and operating on it
r->setSize(5,6); using a pointer.
cout << "area: " << r->area(); Must delete the object once done
delete r;
return 0;
}
object1 = object2;
• This will copy/clone values of each data member
of object2 to corresponding members of object1
void main () {
Rectangle rect1; rect1.setSize(3,4);
Rectangle rect2; rect2.setSize(5,6);
cout << "area1: " << rect1.area();
cout << "area2: " << rect2.area();
rect1 = rect2;
cout << "area1: " << rect1.area();
cout << "area2: " << rect2.area();
rect1.setSize (7,8);
cout << "area1: " << rect1.area();
cout << "area2: " << rect2.area();
}
CT077-3-2-DSTR Data Structures 14
Objects Assignment - Java
What’s the public class Rectangle {
int width, height;
output if we do int area() {
return (width*height); First rectangle
similar thing in } gets de-allocated
Java? will make both of
Assignment
void setSize (int a, int b) {
width = a; height = b;
from memory by
garbage collector
rect1 and rect2 as references }
to the same object, rather public static void main (String [ ] args){
than copying corresponding Rectangle rect1 = new Rectangle();
fields values of rect2 to rect1 Rectangle rect2 = new Rectangle();
rect1.setSize(3,4); rect2.setSize(5,6);
rect1 = rect2;
System.out.println("area1: " +
rect1.area());
Actually changing dimensions System.out.println("area2: " +
of the same object being rect2.area());
referenced by rect1 and rect2 rect1.setSize(7,8);
System.out.println("area1: " +
rect1.area());
System.out.println("area2: " +
CT077-3-2-DSTR
rect2.area());
Data Structures 15
Static Members of a Class
a2.sValue++;
A::sValue++;
Question: Suppose you want to keep track of your books in a library. You might
want to track the following attributes about each book:
Title
Author
Subject
Book ID
Here you can define a structure for grouping all the data in once.
To define a structure, you must use the struct statement. The struct statement defines a new data
type, with more than one member for your program
Question: Suppose you want to keep track of your books in a library. You might
want to track the following attributes about each book:
Title
Author
Subject
Book ID
Here you can define a class for grouping all the data in once.
To define a class, you must use the class statement. The class statement defines a new data type, with
more than one member for your program
Here you can define a structure & a class for grouping all the data in once.
To define a class, you must use the class & struct statement. The class & struct statement defines a
new data type, with more than one member for your program. Use the pointer to create a dynamic
structure.
class flower
{
public :
int flowerid;
string flowername;
string flowervenue;
};
int main()
{
flower * flower1 = new flower ();
flower1->flowerid = 1;
flower1->flowername = "SunFlower";
flower1->flowervenue = "Poland";
return 0;
struct flower
{
int flowerid;
string flowername;
string flowervenue;
};
int main()
{
flower * flower1 = new flower ();
flower1->flowerid = 1;
flower1->flowername = "SunFlower";
flower1->flowervenue = "Poland";
return 0;
~ Polynomial() { … } //destructor
• Linked-list
– Introduction
– Properties