[go: up one dir, main page]

0% found this document useful (0 votes)
2 views6 pages

Exp 4 - (Class and Objects) Yash Mane

The document presents a series of C++ programs demonstrating the use of classes and objects. It includes examples of function declarations inside and outside classes, creating arrays of objects, and defining constructors for class objects. Each program is accompanied by its corresponding output, showcasing fundamental object-oriented programming concepts in C++.

Uploaded by

413 YASH MANE
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)
2 views6 pages

Exp 4 - (Class and Objects) Yash Mane

The document presents a series of C++ programs demonstrating the use of classes and objects. It includes examples of function declarations inside and outside classes, creating arrays of objects, and defining constructors for class objects. Each program is accompanied by its corresponding output, showcasing fundamental object-oriented programming concepts in C++.

Uploaded by

413 YASH MANE
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/ 6

BRANCH : ENTC (4TH SEM)

NAME : YASH MANE


PRN : 2455004

Experiment No. 4 [CLASS & OBJECTS ]

Program 1 - Function declared inside the class


 Code:
#include <iostream>
using namespace std;
class MyClass {
public:
void display() {
cout << "HELLO CPP WORLD" << endl;
}
};
int main() {
MyClass obj; // Object declared
obj.display(); // Calling the function
return 0;
}

 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:

You might also like