Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views21 pages

Classes Part 1

The document provides an overview of structures and classes in programming, highlighting their definitions, differences, and usage. It explains how structures are used to store related data without member functions, while classes encapsulate data and behavior through member functions and access specifiers. Additionally, it covers constructors, accessors, mutators, and the importance of information hiding in object-oriented programming.

Uploaded by

S'fiso Mzilikazi
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)
9 views21 pages

Classes Part 1

The document provides an overview of structures and classes in programming, highlighting their definitions, differences, and usage. It explains how structures are used to store related data without member functions, while classes encapsulate data and behavior through member functions and access specifiers. Additionally, it covers constructors, accessors, mutators, and the importance of information hiding in object-oriented programming.

Uploaded by

S'fiso Mzilikazi
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/ 21

Structures

Structures are used to store multiple logically related


elements.
A structure can be thought of as an object without any
member functions.
Consists of a collection of values of different types
which are treated as a single item.
The keyword struct indicates a structure type definition.
The structure tag indicates the name of a structure type
and can be any legal identifier.
Structure definition
struct StudentStruct
structure name
{
int studentID;
string name; structure members
int year;
double average;
};

N.B.If you have not declared any access specifier, then the members of
the structure are public.
Structure
The identifiers declared inside the brackets, {}, are
called member names.
A structure type definition:
 ends with a bracket, } and a semicolon.
 is usually placed outside of any function definition
The structure type is then available to all the code that
follows the structure definition.
Structure
Once a structure type definition has been given, the
structure type can be used just like the predefined types
int, char.
 For example:
StudentStruct student1, student2
declares two variables, named student1 and student2
of type StudentStruct.
A value of the type StudentStruct is a collection of the
member values: studentID, name, year and average
Class
 A class is a data type whose variables are objects.
 The class definition specifies :
(1) what attributes the data type has i.e. the type of
values the variables can hold and
(2) its behaviour (member functions) i.e. what it can
do.
 When we declare variables of a specific class, we say
we instantiate objects of the class.
Structs and Classes
STRUCT CLASS
User-defined data type: combines logically User-defined data type: set of instructions to build a
related data items of different data types specific type of object [should be defined as an
abstract data type]
Uses keyword struct Uses keyword class
Defines member variables Defines member variables and member functions
Syntax: Syntax:
struct struct_name class class-name
{ {
// struct data members // data
}; // functions
};
Converting a Struct to a Class
### Declaration
class Student
{ public:
Constructor – ALWAYS same name as class.
Student();
void setStudentID(string ID); Mutators
void setName(string nam);
void setYear(int yr);
void setAverage(double a);
int getStudentID() const; Accessors
string getName() const;
double getAverage() const;

private:
Private member variables – cannot be DIRECTLY
int studentID;
accessed outside this class
string name;
int year;
double average;
};
Classes
 The class keyword is used to create a class called Student.
 The public keyword is an access specifier, which specifies
that members (attributes and methods) of the class are
accessible from outside the class. You will learn more about
access specifiers later.
 The variables name, studentID, year and average are
declared inside the class.
 When variables are declared within a class, they are called
attributes.
 Note: end the class definition with a semicolon ;.
Classes
 Once a class is defined, an object (which is just a
variable of the class type) can be declared in the same
way as variables of any other type.
 For example, the following declares two objects of type
Student:
Student studentObject1, studentObject2;
 Sound OOP principle to define all member variables as
private – implementing information hiding.
 Access to these member variables are provided via
special methods.
What is…
 Constructors are special class members which are
called by the compiler every time an object of that
class is instantiated.
 Student studObj1; - default constructor assigns default
values.
 Student studObj2 = Student("MyName", 2, 2023,
34.52); - uses parameterized constructor to assign
values to the object.
Member functions
 Ideal class definition: you should be able to change the details of
how the class is implemented and the only things you should
need to change in any program that uses the class are the
definitions of the member functions.
 To realize this ideal, you must have member functions so that
you
never need to access the member variables directly but access
them only through the member functions.
 Then, if you change the member variables, you need change
only the definitions of the member functions to match your
changes to the member variables, and nothing else in your
programs need change.
What is…
 An accessor - public member function in a class that gets (accesses)
the value of a private member variable.
 A mutator - public member function in a class that sets (mutates) the
value of a private member variable.
 The default constructor - a constructor that either has no parameters
or if it has parameters, all the parameters have default values for
their data types.
 An overloaded constructor - We can have more than one constructor
in a class provided each has a different list of arguments
(overloading).
 Destructor: function typically used to deallocate memory and do
other cleanup functions for a class object and its class members
when the object is destroyed. A destructor is called for a class object
when that object passes out of scope or is explicitly deleted.
What is…
 The scope resolution operator :: is used with a class name, whereas
the dot operator is used with objects (that is, with class variables).
 The scope resolution operator consists of two colons with no space
between them.
 The class name that precedes the scope resolution operator is called a
type
qualifier because it specializes (“qualifies”) the function name to one
particular
type.
For example:
Student :: getName() {

}
Member functions
 Notice that in the function definitions, we use the member names
by themselves without giving the object and dot operator.
 At this point we are simply defining the member functions.
 These definitions will apply to all objects of type Student, but at
this
point we do not know the names of the objects of type Student
that
we will use, so we cannot give their names.
 When the member function is called, for example:
studObj1.getName();
the member name in the function definition is specialized to the
name of the calling object.
The Dot Operator and the Scope
Resolution Operator
Both the dot operator and the scope resolution operator are used
with member names to specify what “thing” they are members of.
You use the scope resolution operator :: to specify the class name
when giving the function definition for a member function. For
example, if we included a function called output, the heading of
the function definition for the member function output would be:
void Student::output()
Remember, the scope resolution operator :: is used with a class
name,
whereas the dot operator is used with an object of that class.
Student class
#include <iostream>

using namespace std;


class Student {
private: string name;
int studentID;
int year;
double average;
public:
Student() {
name = "";
studentID = 0;
year = 0;
average = 0.0;
}
Student class
Student(string n, int i, int y, double a) { //parameterized
constructor
name = n;
studentID = i;
year = y;
average = a;
}
Student class: Mutators
void setName(string n) {
name = n;
}

void setStudentID(int i) {
studentID = i;
}

void setYear(int y) {
year = y;
}

void setAverage(double a) {
average = a;
}
Student class: Accessors
string getName() {
return name;
}
int getYear() {
return year;
}
int getStudentID() {
return studentID;
}
double getAverage() {
return average;
}
};
Student class: Accessors
int main()
{ Student studObj1;
//cout << studObj1.name << endl;
cout <<"The default student object has the following values:\n";
cout <<studObj1.getName() <<" " <<studObj1.getStudentID()<<"
"<<studObj1.getYear() <<" "<<studObj1.getAverage() << endl;
Student studObj2 = Student("MyName", 2, 2023, 34.52);
cout <<"The second student object has the following values:\n";
cout <<studObj2.getName() <<" " <<studObj2.getStudentID()<<"
"<<studObj2.getYear() <<" "<<studObj2.getAverage() << endl;
return 0;
}

You might also like