0% found this document useful (0 votes)
21 views14 pages

Lecture 3 Anatomy of Class

Oobject oriented programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views14 pages

Lecture 3 Anatomy of Class

Oobject oriented programming
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

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:

based on parameter types. Class()


{
this -> value = -1;
• The appropriate constructor is }
Class(int val)
chosen during object creation. {
this -> value = val;
Class object1, object2(100); }
void set_val(int value)
cout << object1.get_val() << endl; {

cout << object2.get_val() << endl; }


this -> value = value;

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>

Memory Leaks using namespace std;

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;

• Automatic variables are cleaned }


int *value;
up when their scope ends. };

• Explicit memory allocation void make_a_leak()


should be matched with proper {
deallocation. Class object(1000);
}

int main()
{
make_a_leak();
}
#include <iostream>

using namespace std;

Destructors class Class {


public:
Class(int val)
{
• Destructors free resources value = new int[val];
cout << "Allocation (" << val << ") done." <<
allocated to objects when they endl;
go out of scope. }
~Class()
• They have the same name as the {
delete [] value;
class prefixed with ~. cout << "Deletion done." << endl;
}
int *value;
};

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy