You are on page 1of 5

Department of AIT-

CSE
Institutional Training
June-July, 2021

Department of AIT-CSE

Daily Worksheet - Day-1


Classes and Objects

Que1: Write a C++ program to declare class called CAR with suitable data member and
member functions. Accept the details of at least 5 cars from users and display the details of
each car using display_car_details() member function.

Code:

#include <iostream>
using namespace std;
class cars_class {
private:
char name[20],colour[10];
float price;
public:
void read() {
cout << "Enter Name of brand :";
cin >> name;
cout << "Enter colour of the car :";
cin >> colour;
cout << "Enter price :";
cin >>price;
}
void displayCarDetails() {
cout << "Name :" << name << endl;
cout << "Colour :" << colour<<endl;
cout<<"price :"<<price<<endl;

}
};
int main() {
int number;
cars_class cars[5];
for(int i=0;i<5;++i){
cout << "\nCar :"<<i+1<< endl;
cars[i].read();
cars[i].displayCarDetails();
}
return 0;

Page 1
Department of AIT-
CSE

Que2: Predict the output of following C++ program

#include<iostream>
using namespace std;
  
class Empty {};
  
int main()
{
  cout << sizeof(Empty);
  return 0;
}

Output:
1

Que3: Assume that an integer and a pointer each takes 4 bytes. Also, assume that there is no
alignment in objects. Predict the output following program.

#include<iostream>
using namespace std;
 
class Test
{
    static int x;
    int *ptr;
    int y;
};
 int main()
{
    Test t;
    cout << sizeof(t) << " ";
    cout << sizeof(Test *);
}

Output:
16 8

Page 2
Department of AIT-
CSE

Que4: Create a class named 'Student' with a string variable 'name' and an integer variable
'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an object of
the class Student.

Code:
#include <iostream>

using namespace std;

class StudentClass {
private:
char name[20]="John";

int roll_no=2;

public:

void print() {

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


cout << "Roll number :" << roll_no<<endl;

}
};

int main() {
StudentClass stu;

stu.print();
}

Que5: Create classes that capture bank customers and bank accounts. A customer has a first
and last name. An account has a customer and a balance. Make objects for two accounts held
by the same customer.

Page 3
Department of AIT-
CSE

Code:

#include <iostream>
using namespace std;
class customer
{
private:
string first_name;
string last_name;
public:
void getname(string, string);
void showname();
};
void customer::getname(string s1, string s2)
{
first_name = s1;
last_name = s2;
}
void customer::showname()
{
cout << first_name <<" "<<last_name<< endl;
}
class account
{
public:
customer ob1;
string customer; int balance;
void get_acc_details(string s, int m)
{
customer = s;
balance = m;
}
void show_account_details()
{
cout << "the account name is " << customer << endl;
cout << "the account balance is " << balance << endl;
}
};
int main()
{
account ob2;
ob2.ob1.getname("Raghav","Agarwal");
ob2.ob1.showname();
ob2.get_acc_details("Raghav Agarwal",98765);
ob2.show_account_details();
return 0;
}

Page 4
Department of AIT-
CSE

Page 5

You might also like