Object-Oriented Programming – Abstraction, Classes, Objects & Static Members
Abstraction
One of the fundamental mechanisms for coping with software complexity.
Definition
An abstraction denotes only the essential properties and behaviours that differentiate an object from others.
Omits irrelevant, accidental or context-dependent detail.
Importance
Allows the designer to reason about what an entity is/does before worrying about how it is implemented.
Provides the mental basis for all other OOP techniques (encapsulation, inheritance, polymorphism, etc.).
Essence of OOP
Object-oriented programming is largely the practice of modelling abstractions with classes and objects.
Every software artefact you define (class, interface, module) should correspond to a mental abstraction you want to manipulate.
Classes
Primary language construct for representing an abstraction.
Formal definition (from slide)
A class models an abstraction by defining the properties (data) and behaviours (operations) shared by all its objects.
Key terminology
Data member / Attribute / Field / Data item: variable placed inside a class to store a property value.
Member function / Method / Operation: function placed inside a class to realise a behaviour.
Mathematical view
A class is a set: \text{Class} = {\text{all possible instances meeting the specification}}
General form of a C++ class declaration
class class_name {
private:
// variable declarations
// function declarations
public:
// variable declarations
// function declarations
};
privatesection is listed first by convention but can appear in any order.A trailing semicolon is mandatory after the closing brace.
Class‐versus‐struct parallel
Both supply only a template; no memory is allocated until objects are instantiated.
Default visibility differs (private for class, public for struct).
Objects
Definition: An object is an instance of a class.
Terminology
Instantiation: the act of creating objects from a class.
Syntax parallels basic types:
cpp bus_info x; // create one object bus_info x, y, z; // create three objects
Properties of an object are locked inside until accessed through the object’s member functions (data hiding).
Encapsulation & Data Hiding
Encapsulation: wrapping data + functions that operate on that data into a single unit (class).
If data are
private, they are insulated from direct external access → information hiding.Benefits
Prevents accidental corruption.
Presents a clean interface; implementation can evolve independently.
Abstract Data Type (ADT)
A class whose behaviour is defined by its public operations while its implementation details remain hidden.
In C++, every well-designed class is an ADT.
Worked Example – class bus_info
class bus_info {
private:
int bus_id;
string bustype;
int no_of_seats;
public:
void getdata();
void putdata();
};
Data members (
bus_id,bustype,no_of_seats) are private; only accessible viagetdataandputdata.Demonstrates strict information hiding.
Object creation in practice
bus_info x; // single object
bus_info x, y, z; // multiple objects
Equivalent to: \text{int a;} in syntax mood.
Accessing members
x.getdata(1, "AC", 55); // message: initialise
x.putdata(); // message: display
General syntax:
object_name.function_name(actual_arguments);The dot operator (.) acts as an explicit message send.
Message Passing
Conceptual viewpoint: every function call on an object is a message asking it to perform an operation.
Supports a high-level, anthropomorphic reading of code (objects “know” what to do with the data they own).
Sample visibility illustration
class xyz {
private:
int x, y;
public:
int z;
};
xyz p;
p.x = 10; // ERROR – private
p.z = 10; // OK – public
Example underscores why public data should be rare; defeats encapsulation.
Detailed Learning Objectives (second half)
Defining member functions (inside vs. outside class).
Special characteristics of member functions.
Inline functions.
Nesting of member functions.
Private member functions.
Arrays within a class.
Static data members & static member functions.
Defining Member Functions (outside class body)
return_type class_name::function_name(argument_list) {
// body
}
Example for bus_info:
void bus_info::getdata(int bno, string btype, int n) {
bus_id = bno;
bustype = btype;
no_of_seats = n;
}
void bus_info::putdata() {
cout << bus_id << bustype << no_of_seats;
}
Special characteristics
Member-scope resolution: the
::“membership label” resolves identical function names across different classes.Member functions can freely access private data of their own class.
One member function may call another directly (no dot operator required).
Inline semantics
If a member function is defined inside the class definition, it is treated as
inlineautomatically.Example:
class bus_info {
public:
void putdata() {
cout << bus_id << bustype << no_of_seats;
}
};
To make an external definition inline, prepend
inline:
inline void bus_info::putdata() { /*...*/ }
Goal: eliminate call overhead for tiny functions, but beware of code bloat.
Nesting of member functions
Illustration (
setclass):
void set::display() {
cout << largest(); // direct call; no object.
}
Private Member Functions
Use‐cases: sensitive operations (delete an account, apply salary increment, etc.).
Rule: can be called only by other member functions of the same class.
Example skeleton:
class sample {
int m;
void read(); // private by default
public:
void update();
void write();
};
void sample::update() {
read(); // legal – same class
}
sample s1;
s1.read(); // ILLEGAL – read is private
Arrays as Data Members
const int size = 10;
class array {
int a[size];
public:
void setval();
void display();
};
Each object owns its own array of ten integers.
Static Data Members
Declared with keyword
staticinside class.Characteristics
Single shared copy → "class variable".
Automatically initialised to 0 once the first object is created.
Lives even if no objects remain.
Definition outside class (mandatory):
int product::count; // default 0
// or with an initial value
int product::count = 10; // user-defined init
Why use static data members?
Maintain information common to the entire class (e.g., object counters, configuration flags).
Example – product
class product {
static int count;
int number;
public:
void getdata(int a) {
number = a;
count++; // accesses shared counter
}
void getcount() { cout << count; }
};
int product::count; // allocate storage
void main() {
product a, b, c;
a.getcount(); b.getcount(); c.getcount(); // prints 0 three times
a.getdata(100);
b.getdata(200);
c.getdata(300);
a.getcount(); b.getcount(); c.getcount(); // prints 3 three times
}
Observe that every object sees same value of
count.
Properties recap
Static members are stored separately from object memory.
Access rule for static functions
May only access other static members.
Can be invoked via class name:
class_name::function_name();.
Counter-example (invalid)
static void showcount() {
cout << code; // ERROR – 'code' is non-static
}
Full static demo – test
class test {
static int count;
int code;
public:
void setcode() { code = ++count; }
void showcode() { cout << code; }
static void showcount() { cout << count; }
};
int test::count; // allocate
void main() {
test t1, t2;
t1.setcode();
t2.setcode();
test::showcount(); // prints 2
test t3;
t3.setcode();
test::showcount(); // prints 3
t1.showcode(); t2.showcode(); t3.showcode(); // 1 2 3
}
Arrays of Objects
You may declare an array whose element type is a class.
class employee {
char name[30];
float age;
public:
void getdata();
void putdata();
} manager[30]; // 30 objects created at once
Use common for modelling collections (staff roster, inventory list, etc.).
Conceptual Connections & Real-World Relevance
Abstraction & ADTs → datatypes like
std::vector, database "Record", GUI "Button".Encapsulation parallels real-world objects: a bus exposes seats availability, hides engine mechanics.
Static class variables resemble organisation-wide counters (employee IDs, global configuration flags).
Message passing paradigm underlies distributed systems (remote procedure calls, microservices) and UI event dispatch.
Inline functions reflect common performance trade-off: time vs. space.
Private member functions implement the principle of least privilege, crucial for security.
Ethical / Practical Implications
Proper data hiding prevents unintended misuse that could breach privacy (e.g., employee salary details).
Encapsulation supports maintenance and evolution; code bases become safer to extend.
Quick Reference Equations & Syntax
Syntax markers
Class scope resolution:
class_name::member.Message passing:
object.member(args);.
Static initialisation with value:
int product::count = 10;count = 10.Increment and assignment example:
code = ++count;code = count + 1 before assignment.
Use these organised notes as a full substitute for the original 43-slide transcript when revising OOP fundamentals in C++.