Name – SHASHWAT TIWARI
CSE (CN)
Week 3
18CSC202J – Object Oriented Design and Programming
1. 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.
ANS-
PROGRAM:
#include <iostream>
#include <string>
using namespace std;
class student {
public:
string name;
int roll_no;
};
int main() {
student st1;
st1.name = "john";
st1.roll_no = 2;
cout << "Name: " << st1.name << endl;
cout << "roll: " << st1.roll_no << endl;
}
, 2. Write a C++ program to illustrate the concept of class and object creation. (Ask
students to create a class, methods and invoke them inside the main method).
ANS-
PROGRAM:
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
// Access specifier
public:
// Data Members
string geekname;
// Member Functions()
void printname()
{
cout << "Geekname is: " << geekname;
}
};
int main() {
// Declare an object of class geeks
Geeks obj1;
// accessing data member
obj1.geekname = "sakshi";
// accessing member function
CSE (CN)
Week 3
18CSC202J – Object Oriented Design and Programming
1. 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.
ANS-
PROGRAM:
#include <iostream>
#include <string>
using namespace std;
class student {
public:
string name;
int roll_no;
};
int main() {
student st1;
st1.name = "john";
st1.roll_no = 2;
cout << "Name: " << st1.name << endl;
cout << "roll: " << st1.roll_no << endl;
}
, 2. Write a C++ program to illustrate the concept of class and object creation. (Ask
students to create a class, methods and invoke them inside the main method).
ANS-
PROGRAM:
#include <bits/stdc++.h>
using namespace std;
class Geeks
{
// Access specifier
public:
// Data Members
string geekname;
// Member Functions()
void printname()
{
cout << "Geekname is: " << geekname;
}
};
int main() {
// Declare an object of class geeks
Geeks obj1;
// accessing data member
obj1.geekname = "sakshi";
// accessing member function