Professional Documents
Culture Documents
UNIT-4
By
ADITYA BHARDWAJ
1
• Till now we have seen fundamentals of C++
programming.
Objects Name Attributes/ Behaviors/
Properties Functionality/
Actions
Human Beings Age, color, weight, Walking, talking,
etc. sleeping, doing
running etc.
Syntax:
• ClassName ObjectName; // Declare object of type
class (eg. car object of type vehicle, vivo phone
object of type mobile)
Point 3. Accessing data members and
member functions:
• The data members and member functions of
class can be accessed using the dot(‘.’)
operator with the object.
• For example if the name of object is obj and
you want to access the member function with
the name running() then you will have to
write obj.running() .
Finally Basic Structure of Class and object
Example 1. Person Object
Observations
• Object data type name is same as class name. It
means object is declared of class datatype.
• By default member function is defined inside
class, but can we define membership function
outside the class ?.
• By default C++ follows private access specifier.
• Data member from class blue print are accessed
using dot (.) operator in main function. Syntax:
Objectname.datamember name
Give Answer for the following
• What is class blueprint in previous question.
• What is data members.
• What is member function.
• Where objects are declared
• Where data members and member functions
are accessed.
2. To Calculate volume of box =
length*breadth*height
Give Answer for the following
• What is class blueprint in previous question.
• What is data members.
• What is member function.
• Where objects are declared
• Where data members and member functions
are accessed.
Example 3. Human Being
4. Concept of C++ Class with Array using Students details Example
Example 5. Lets consider the Scenario of creating a
PECStudent Class
• class PECStudent
• Objects (instances of the class): Student1,
Student2, Student3 etc
• Data members of this class can be Name, Roll
Number, Marks etc.
• Member Functions: Average Marks, Highest
Marks etc
#include <iostream>
#include<cstring> How to access the data members of
using namespace std;
a class inside main function ??
class PECStudent
{
public:
string name;
int studentID;
};
class A {
public:
int a=3;
void display()
{ cout<<a; } };
int main() {
A a;
a.display();
return 0; }
Output: 3
Memory Allocation for Data Member and
Functions
Once you define class it will not allocate memory space for
the data member of the class. The memory allocation for the
data member of the class is performed separately each time
when an object of the class is created.
Since member functions defined inside class remains same for all
objects, only memory allocation of member function is performed
at the time of defining the class.