Chapter 2. Class and Objects
Chapter 2. Class and Objects
Output
1/25/2024
Classes and Objects, OOP in C++ 6
Limitation of Structure
1. Does not allow the struct data type to be treated like built-in datatypes.
2. No data hiding
3. No functions and constructors can be placed inside structure
4. Cannot have static members inside it
Object
• Is an instance of class
//Class declaration
OR
// Objects creation
just after
class definition
// Objects creation
beyond
class definition
// class declaration
// data members
// function member
// function member
Class Box
{
void showVolume()
{
……….//definition
}
};
Class Box
{
};
void showVolume()
{
……….//definition
}
class Rectangle{
private:
int l,b,a;
public:
//Function defination inside class
void getData(){
cout<<"Enter length and breadth"<<endl;
cin>>l>>b;
}
// function declaration for function defined outside class
void showData();
};
//Function defination outside class
void Rectangle:: showData(){
a=l*b;
cout<<"Area is "<<a<<endl;
}
int main() {
Rectangle ob;
ob.getData();
ob.showData();
}
void display() {
cout << "Name: " << name << ", Age: " << age << endl;
}
};
int main() {
const int arraySize = 3;
Person p[arraySize];
p[0].name = "Alice";
p[0].age = 25;
p[1].name = "Bob";
p[1].age = 30;
p[2].name = "Charlie";
p[2].age = 35;
Association: Composition:
Source:
https://www.tutorialspoint.com/uml
/uml_class_diagram.htm
Constructor
MESSAGE, INSTANCE AND INITIALIZATION ,
1/25/2024 43
OOP in C++
Constructors : Sample program
constructor
Copy
Default Paramaterized
constructors
Example:
• Structure:
class friend_class
{
};
• Where:
– regular_variable is a variable that has already initialized,
– reference_variable is an alternative name (alias) to
represent the variable regular_variable