0% found this document useful (0 votes)
7 views4 pages

Assignment 2 Oop Lab

The document contains three C++ programs demonstrating object-oriented programming concepts. The first program defines a Car class with methods to set and display car details, the second defines a Student class with a constructor that initializes attributes and displays a message upon creation, and the third illustrates the use of constructors and destructors in a Test class, showing when the destructor is called. Each program includes a main function that creates objects and displays their details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Assignment 2 Oop Lab

The document contains three C++ programs demonstrating object-oriented programming concepts. The first program defines a Car class with methods to set and display car details, the second defines a Student class with a constructor that initializes attributes and displays a message upon creation, and the third illustrates the use of constructors and destructors in a Test class, showing when the destructor is called. Each program includes a main function that creates objects and displays their details.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Ahmad Ibrahim

Roll No: BSCS51F24S047


OOP LAB ASSIGNMENT NO.1
/**Write a program to define a class Car with attributes brand and model.

Create objects of the class, assign values, and display the car details using a method.*/

#include <iostream>

using namespace std;

class Car {

private:

string brand;

string model;

public:

void setCarDetails(string b, string m)

brand = b;

model = m;

void displayCarDetails()

cout<<"Car Brand: "<<brand<<endl;

cout<<"Car Model: "<<model<<endl;

};

int main()

Car car1,car2;
car1.setCarDetails("Toyota", "Corolla");

car2.setCarDetails("Honda", "Civic");

cout << "Car 1 Details:" << endl;

car1.displayCarDetails();

cout << "\nCar 2 Details:" << endl;

car2.displayCarDetails();

return 0;

/**Write a program to define a Student class with a constructor that initializes the student’s name
and grade.

Create objects and display a message when an object is created.*/

#include <iostream>

using namespace std;

class Student

private:

string name;

char grade;

public:

Student(string n, char g)

name = n;

grade = g;

cout<<"Student object created for: "<<name<<endl;

}
void displayDetails()

cout<<"Student Name: "<<name<<endl;

cout<<"Grade: "<<grade<<endl;

};

int main()

Student s1("Ali", 'A');

Student s2("Sara", 'B');

cout<<"\nDetails of Student 1:"<<endl;

s1.displayDetails();

cout<<"\nDetails of Student 2:"<<endl;

s2.displayDetails();

return 0;

/**Write a program to define a Test class with a constructor and a destructor.

Create objects inside and outside a scope, and observe when the destructor is called.*/

#include <iostream>

using namespace std;

class Test

public:

Test()

{
cout<<"Constructor called!"<<endl;

~Test()

cout<<"Destructor called!"<<endl;

};

int main()

cout<<"Creating object t1 outside scope..."<<endl;

Test t1;

cout<<"Entering a new scope..."<<endl;

Test t2;

cout<<"Exiting the scope..."<<endl;

cout<<"End of main function..."<<endl;

return 0;

You might also like