0% found this document useful (0 votes)
4 views

Cpp OOP Programs

The document contains C++ programs demonstrating various Object-Oriented Programming (OOP) concepts. It includes examples of class creation, constructor overloading, single inheritance, and encapsulation using a Student class. Each program is accompanied by its main function to illustrate the concepts in action.

Uploaded by

Kunal Dhiman
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)
4 views

Cpp OOP Programs

The document contains C++ programs demonstrating various Object-Oriented Programming (OOP) concepts. It includes examples of class creation, constructor overloading, single inheritance, and encapsulation using a Student class. Each program is accompanied by its main function to illustrate the concepts in action.

Uploaded by

Kunal Dhiman
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/ 2

C++ Programs - OOP Concepts

Q1. Create a class Student with a constructor that prints "Object Created".
#include <iostream>
using namespace std;

class Student {
public:
Student() {
cout << "Object Created" << endl;
}
};

int main() {
Student s;
return 0;
}

Q2. Write a program to demonstrate Constructor Overloading.


#include <iostream>
using namespace std;

class Student {
public:
Student() {
cout << "Default Constructor" << endl;
}

Student(string name) {
cout << "Name: " << name << endl;
}
};

int main() {
Student s1;
Student s2("Anu");
return 0;
}

Q3. Program for Single Inheritance using class Student and Marks.
#include <iostream>
using namespace std;

class Student {
public:
void sayHi() {
cout << "Hi from Student" << endl;
}
};
class Marks : public Student {
public:
void showMarks() {
cout << "Marks: 90" << endl;
}
};

int main() {
Marks m;
m.sayHi();
m.showMarks();
return 0;
}

Q4. Create a class Student with private data and use Encapsulation to set and get it.
#include <iostream>
using namespace std;

class Student {
private:
int age;

public:
void setAge(int a) {
age = a;
}

int getAge() {
return age;
}
};

int main() {
Student s;
s.setAge(18);
cout << "Age is: " << s.getAge();
return 0;
}

You might also like