0% found this document useful (0 votes)
3 views

Classes Objects

The document provides an overview of classes and objects in C++, highlighting key concepts such as attributes, methods, constructors, access specifiers, encapsulation, inheritance, polymorphism, and destructors. It explains how to create classes and objects, define methods, and manage access to class members. Additionally, it covers the importance of encapsulation for data security and the role of inheritance and polymorphism in code reusability and flexibility.

Uploaded by

jgr102005
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)
3 views

Classes Objects

The document provides an overview of classes and objects in C++, highlighting key concepts such as attributes, methods, constructors, access specifiers, encapsulation, inheritance, polymorphism, and destructors. It explains how to create classes and objects, define methods, and manage access to class members. Additionally, it covers the importance of encapsulation for data security and the role of inheritance and polymorphism in code reusability and flexibility.

Uploaded by

jgr102005
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/ 18

Classes & Objects

Intro

 C++ is an object-oriented programming language.


 Everything in C++ is associated with classes and objects, along with
its attributes and methods. For example: in real life, a car is
an object. The car has attributes, such as weight and color,
and methods, such as drive and brake.
 Attributes and methods are basically variables and functions that
belong to the class. These are often referred to as "class members".
 A class is a user-defined data type that we can use in our program,
and it works as an object constructor, or a "blueprint" for creating
objects.
Create a Class

class MyClass { // The class


public: // Access specifier
int myNum; // Attribute (int variable)
string myString; // Attribute (string variable)
};
 The class keyword is used to create a class called MyClass.
 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.
 Inside the class, there is an integer variable myNum and a string
variable myString. When variables are declared within a class, they are
called attributes.
 At last, end the class definition with a semicolon “;”.
Create an Object

 In C++, an object is created from a class. We have already created the


class named MyClass, so now we can use this to create objects.
 To create an object of MyClass, specify the class name, followed by the
object name.
 You can create multiple objects of one class.
 To access the class attributes (myNum and myString), use the dot
syntax (.) on the object:
MyClass myObj; // Create an object of MyClass

// Access attributes and set values


myObj.myNum = 15;
myObj.myString = "Some text";

// Print attribute values


cout << myObj.myNum << "\n";
Class Methods

 Methods are functions that belongs to the class.


 There are two ways to define functions that belong to a class:
 Inside class definition
 Outside class definition
class MyClass { // The class
public: // Access specifier
void myMethod() { // Method/function defined inside the
class
cout << "Hello World!";
}
};

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
Class Methods
To define a function outside the class definition, you have to declare it
inside the class and then define it outside of the class. This is done by
specifying the name of the class, followed the scope resolution ::
operator, followed by the name of the function:
class MyClass { // The class
public: // Access specifier
void myMethod(); // Method/function declaration
};

// Method/function definition outside the class


void MyClass::myMethod() {
cout << "Hello World!";
}

int main() {
MyClass myObj; // Create an object of MyClass
myObj.myMethod(); // Call the method
return 0;
}
Constructors

A constructor in C++ is a special method that is automatically called when an object of a


class is created.
To create a constructor, use the same name as the class, followed by parentheses ():
class MyClass { // The class
public: // Access specifier
MyClass() { // Constructor
cout << "Hello World!";
}
};

int main() {
MyClass myObj; // Create an object of MyClass (this will call the
constructor)
return 0;
}
Constructor Parameters
Constructors can also take parameters (just like regular functions), which can be useful for
setting initial values for attributes.
class Car { // The class
public: // Access specifier
string brand; // Attribute
string model; // Attribute
int year; // Attribute
Car(string x, string y, int z) { // Constructor with parameters
brand = x;
model = y;
year = z;
}
};

int main() {
// Create Car objects and call the constructor with different values
Car carObj1("BMW", "X5", 2020);
Car carObj2("Ford", "Mustang", 1969);

// Print values
cout << carObj1.brand << " " << carObj1.model << " " << carObj1.year << "\n";
cout << carObj2.brand << " " << carObj2.model << " " << carObj2.year << "\n";
return 0;
}
Access Specifiers

 The public keyword is an access specifier. Access specifiers define how the
members (attributes and methods) of a class can be accessed. In the
example above, the members are public - which means that they can be
accessed and modified from outside the code.
 However, what if we want members to be private and hidden from the
outside world?
 In C++, there are three access specifiers:
 public - members are accessible from outside the class
 private - members cannot be accessed (or viewed) from outside the class
 protected - members cannot be accessed from outside the class, however, they
can be accessed in inherited classes. You will learn more about Inheritance later.
 By default, all members of a class are private if you don't specify an
access specifier
 In the following example, we demonstrate the differences between public
and private members:
Access Specifiers Example

class MyClass {
public: // Public access specifier
int x; // Public attribute
private: // Private access specifier
int y; // Private attribute
};

int main() {
MyClass myObj;
myObj.x = 25; // Allowed (public)
myObj.y = 50; // Not allowed (private)
return 0;
}
Encapsulation

 The meaning of Encapsulation is to make sure that "sensitive" data is


hidden from users. To achieve this, you must declare class
variables/attributes as private (cannot be accessed from outside the
class). If you want others to read or modify the value of a private
member, you can provide public get and set methods.
 It is considered good practice to declare your class attributes as
private (as often as you can). Encapsulation ensures better control of
your data, because you (or others) can change one part of the code
without affecting other parts
 Increased security of data
Inheritance
 In C++, it is possible to inherit attributes and methods from one class
to another. We group the "inheritance concept" into two categories:
 derived class (child) - the class that inherits from another class
 base class (parent) - the class being inherited from
 To inherit from a class, use the : symbol.
 It is useful for code reusability: reuse attributes and methods of an
existing class when you create a new class.
 Check the next slide for an example.
// Base class
class Vehicle {
public:
string brand = "Mercedes";
void honk() {
cout << "Tuut, tuut! \n" ;
}
};

// Derived class
class Car: public Vehicle {
public:
string model = "CLS";
};

int main() {
Car myCar;
myCar.honk();
cout << myCar.brand + " " + myCar.model;
return 0;
}
Note About Inheritance

 When a base class is privately inherited by the derived class, public


members of the base class becomes the private members of the
derived class and therefore, the public members of the base class can
only be accessed by the member functions of the derived class. They
are inaccessible to the objects of the derived class.
 On the other hand, when the base class is publicly inherited by the
derived class, public members of the base class also become the
public members of the derived class. Therefore, the public members
of the base class are accessible by the objects of the derived class as
well as by the member functions of the derived class.
Polymorphism

 Polymorphism means "many forms", and it occurs when we have


many classes that are related to each other by inheritance.
 Inheritance lets us inherit attributes and methods from another class.
Polymorphism uses those methods to perform different tasks. This
allows us to perform a single action in different ways.
 For example, think of a base class called Animal that has a method
called animalSound(). Derived classes of Animals could be Pigs, Cats,
Dogs, Birds - And they also have their own implementation of an
animal sound (the pig oinks, and the cat meows, etc.).
 Check the next slide for an example.
class Animal {
public:
void animalSound() {
cout << "The animal makes a sound \n";
}
};

// Derived class
class Pig : public Animal {
public:
void animalSound() {
cout << "The pig says: wee wee \n";
}
};

// Derived class
class Dog : public Animal {
public:
void animalSound() {
cout << "The dog says: bow wow \n";
}
};
Destructor

 Destructor is an instance member function which is invoked automatically whenever


an object is going to be destroyed. Meaning, a destructor is the last function that is
going to be called before an object is destroyed.
 Destructor is also a special member function like constructor. Destructor destroys
the class objects created by constructor.
 Destructor has the same name as their class name preceded by a tilde (~) symbol.
 It is not possible to define more than one destructor.
 The destructor is only one way to destroy the object created by constructor. Hence
destructor cannot be overloaded.
 Destructor neither requires any argument nor returns any value.
 It is automatically called when object goes out of scope.
 Destructor release memory space occupied by the objects created by constructor.
 In destructor, objects are destroyed in the reverse of an object creation.
class Test
{
public:
Test()
{
cout<<"\n Constructor executed";
}
~Test()
{
cout<<"\n Destructor executed";
}
};
main()
{
Test t1,t2,t3; return 0;
}

You might also like