Exp 4 - (Class and Objects) Yash Mane
Exp 4 - (Class and Objects) Yash Mane
Output:
Program 2 – Function declared outside the class
Code:
#include <iostream>
using namespace std;
class MyClass {
public:
void display(); // Function declaration inside the class
};
void MyClass::display() {
cout << "YASH MANE" << endl;
}
int main() {
MyClass obj; // Object declared
obj.display(); // Calling the function
return 0;
}
Output:
Program 3 – Array of Objects
Code:
#include <iostream>
using namespace std;
class MyClass {
public:
void display() {
cout << "I LOVE RIT" << endl;
}
};
int main() {
MyClass arr[5]; // Array of objects
for (int i = 0; i < 5; i++) {
arr[i].display(); // Calling the function for each object
}
return 0;
}
Output:
Program 4 – Create an object and call the function
Code:
#include <iostream>
using namespace std;
class Dog {
public:
// Function to make the dog bark
void bark() {
cout << "Bhow Bhow!" << endl;
}
};
int main() {
Dog dog; // Create an object of the Dog class
dog.bark(); // Call the bark()
return 0;
}
Output:
Program 5 – Create an object and call the function (2)
Code
#include <iostream>
#include <string>
using namespace std;
class Student {
private:
string name; // Name of the student
int rollNumber; // Roll number of the student
public:
Student(string studentName, int studentRollNumber) : name(studentName),
rollNumber(studentRollNumber) {}
void display() {
cout << "Name: " << name << ", Roll Number: " << rollNumber << endl;
}
};
int main() {
Student student1("YASH MANE", 2455004); // First student object
Student student2("ANIRUDH PATIL", 2455008); // Second student object
Student student3("ABDUL NADAF",2455015); // 3rd Student object
student1.display();
student2.display();
student3.display();
return 0;
}
Output: