Lecture 3 Anatomy of Class
Lecture 3 Anatomy of Class
Class Components
• A class is an aggregate consisting of variables (fields or properties)
and functions (methods). Both variables and functions are class
components.
• Variables:
• A class contains variables like int value;.
• They can also be called fields or properties.
• Functions:
• Functions in a class are sometimes referred to as methods.
• Examples are void set_val(int value); and int get_val();.
Access Specifiers
• class Class { • Public vs. Private:
• public: • Public components are
• void set_val(int value); accessible to all users of the
class.
• int get_val();
• Private components are only
• private: accessible within the class.
• int value;
• };
• Objects of a class have all components defined in the class.
• Public components are accessible.
• Private components are hidden and cannot be accessed directly.
Class the_object;
the_object.set_val(0); // Public component usage
the_object.value = 0; // Error: Private component not accessible
Overriding Component Names
• Functions in a class have access class Class {
to other class components, public:
including private ones. void set_val(int value)
• Naming conflicts can occur when {
function parameters have the Class::value = value;
same name as class }
components. int get_val();
private:
• Use qualification with the home int value;
class name to access overridden };
names.
The 'this' Pointer
• Each object has a special class Class {
component called this. public:
• It's a pointer to the current void set_val(int value)
object and can be used to access {
object components. this -> value = value;
}
• Access object components using int get_val();
this->component_name. private:
int value;
};
Qualifying Component Names
• Functions defined outside the class Class {
public:
class body must be qualified void set_val(int value)
with the home class name and {
the :: operator. this -> value = value;
}
• They have full access to all class int get_val();
components. private:
int value;
};
int Class::get_val()
{
return value;
}
Overloading Class Functions
• Class functions can be class Class {
public:
overloaded, similar to ordinary void set_val(int value)
functions. {
this -> value = value;
• Default parameter values can be }
void set_val()
used in class functions. {
value = -2;
• Verification example: }
Class object; int get_val()
cout << object.get_val() << endl; {
return value;
}
private:
int value;
};
Constructors
class Class {
public:
• Constructors are functions with
Class() the same name as their home
{
this -> value = -1;
class.
}
void set_val(int value)
• They initialize object
{ components during object
}
this -> value = value; creation.
int get_val() • Constructors cannot have return
{
return value; type specifications.
}
private:
int value;
};
Overloading Constructors
• Constructors can be overloaded class Class {
public:
int get_val()
Ouput: {
return value;
-1 }
private:
100 int value;
};
Copying Constructors
#include <iostream>
• Copying constructors copy data using namespace std;
from one object to another of class Class1 {
public:
the same class. Class1(int val)
{
• They are invoked during object this -> value = val;
declaration with an initiator. }
Class1(Class1 const &source)
{
value = source.value + 100;
}
int value;
};
int main()
{
Class1 object11(100), object12 = object11;
cout << object12.value << endl;
#include <iostream>
class Class {
public:
• Memory leaks occur when Class(int val)
memory allocated to objects is {
value = new int[val];
not properly released. cout << "Allocation (" << val << ") done." << endl;
int main()
{
make_a_leak();
}
#include <iostream>
void make_a_leak()
{
Class object(1000);
}
int main()
{
make_a_leak();
Assignment for Attendance of this online
class
CPPE1: Module 1 Test Must be completed on
netacad.com