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

22 Classes-and-Objects-in-CPP

The document explains the concepts of classes and objects in C++, highlighting that a class is a user-defined data type that encapsulates data members and member functions. It details how to define a class, declare objects, and access data members using access modifiers, along with examples of member functions defined both inside and outside the class. The document provides code snippets to illustrate these concepts in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views5 pages

22 Classes-and-Objects-in-CPP

The document explains the concepts of classes and objects in C++, highlighting that a class is a user-defined data type that encapsulates data members and member functions. It details how to define a class, declare objects, and access data members using access modifiers, along with examples of member functions defined both inside and outside the class. The document provides code snippets to illustrate these concepts in practice.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

C++ Programming

Topperworld.in

Classes and Objects

• Class in C++ is the building block that leads to Object-Oriented


programming. It is a user-defined data type, which holds its own data
members and member functions, which can be accessed and used by
creating an instance of that class.

• A C++ class is like a blueprint for an object.

• A Class is a user-defined data type that has data members and member
functions.
• Data members are the data variables and member functions are the
functions used to manipulate these variables together, these data
members and member functions define the properties and behavior of
the objects in a Class.
An Object is an instance of a Class. When a class is defined, no memory
is allocated but when it is instantiated (i.e. an object is created)
memory is allocated.

❖ Defining Class and Declaring Objects

©Topperworld
C++ Programming

➢ A class is defined in C++ using the keyword class followed by the name
of the class.
➢ The body of the class is defined inside the curly brackets and terminated
by a semicolon at the end.

❖ Declaring Objects

When a class is defined, only the specification for the object is defined; no
memory or storage is allocated. To use the data and access functions defined
in the class, you need to create objects.

Syntax:
ClassName ObjectName;

❖ Accessing Data Members


⚫ The public data members are also accessed in the same way given
however the private data members are not allowed to be accessed
directly by the object.
⚫ Accessing a data member depends solely on the access control of that
data member.
⚫ This access control is given by Access modifiers in C++. There are three
access modifiers: public, private, and protected.
Example:

#include <iostream>
class Student {
public:
// Data members

std::string name;
int age;

©Topperworld
C++ Programming

// Constructor
Student(std::string n, int a) {
name = n;
age = a;
}
// Member function to display student information
void displayInfo() {
std::cout << "Name: " << name << "\n";
std::cout << "Age: " << age << "\n";
}
};
int main() {
// Creating an object of the Student class
Student student1("Alice", 20);

// Accessing data members using object


std::cout << "Using object:\n";
std::cout << "Name: " << student1.name << "\n";
std::cout << "Age: " << student1.age << "\n";

// Accessing data members using member function


std::cout << "\nUsing member function:\n";
student1.displayInfo();
return 0;
}

©Topperworld
C++ Programming

Output:

Using object:
Name: Alice
Age: 20
Using member function:
Name: Alice
Age: 20

❖ Member Functions in Classes


There are 2 ways to define a member function:
• Inside class definition
• Outside class definition
To define a member function outside the class definition we have to use
the scope resolution:: operator along with the class name and function
name.
Example:
#include <iostream>
class MyClass;
void outsideFunction(MyClass& obj);
class MyClass {
public:
void insideFunction() {
std::cout << "Inside class function." << std::endl;
}
};

void outsideFunction(MyClass& obj) {


std::cout << "Outside class function." << std::endl;
}
©Topperworld
C++ Programming

int main() {
MyClass obj;
obj.insideFunction();
outsideFunction(obj);
return 0;

Output:
Inside class function.
Outside class function.

©Topperworld

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