Object Oriented Programming Using C

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

OBJECT ORIENTED PROGRAMMING USING C++ LABORATORY

Course Code 23PLCS23 CIE Marks 50


SEE Marks 50
Course Type (Theory/Practical /Integrated) Integrated
Total Marks 100
Teaching Hours/Week (L: T:P: S) 2:0:2:0 Exam Hours 03
Total Hours of Pedagogy 40 Credits 03

How to Execute C++ Program in Ubuntu

Step 1: create a folder under your name in D drive or desktop


Eg: cd desktop/
Step 2: Create a filename
Eg: Geetha.cpp
Step3 save the program
Step 4: g++ Geetha.cpp
Step 5: ./a.out
PROGRAM-1
Write a C++ program to declare structure. Initialize and display contents of
member
Variables
Program:

#include <iostream>
#include <string>

using namespace std;

// Define the structure


struct student
{
string name;
int rollno;
float marks;
};

int main()
{
// Declare a variable of type Person
struct student s;

cout<<" Enter the Student Information \n";


cout<<"Name of the student: ";
cin>>s.name;
cout<<"Roll No: ";
cin>>s.rollno;
cout<<"Marks: ";
cin>>s.marks;

// Display the contents of member variables


cout << "Student's Name: " << s.name << endl;
cout << "Student's Rollno: " << s.rollno << endl;
cout << "Student's Marks: " << s.marks<< endl;
}

OUTPUT:

Enter the Student Information

Name of the student: Arun

Roll No: 23

Marks: 35.7
Student's Name: Arun

Student's Rollno: 23

Student's Marks: 35.7


PROGRAM-2
Write a C++ program to display Names, Roll No., and grade of N students who
have appeared in the examination. Declare the class of name, roll no., and
grade. Create an array of class objects. Read and display the contents of the
array.
Program:

#include <iostream>
#include <string>

using namespace std;

class Student
{
public:
string name;
int rollNo;
char grade;

// Member function to input student details


void input()
{
cout << "Enter name: ";
cin >> name;
cout << "Enter roll number: ";
cin >> rollNo;
cout << "Enter grade: ";
cin >> grade;
}

// Member function to display student details


void display()
{
cout << "Name: " << name << ", Roll No.: " << rollNo << ", Grade: " <<
grade << endl;
}
};

int main()
{
int n;

cout << "Enter the number of students: ";


cin >> n;

// Create an array of class objects


Student students[n];
// Input details for each student
for (int i = 0; i < n; ++i)
{
cout << "Enter details for student " << i + 1 << ":" << endl;
students[i].input();
}

cout << "\nStudent Details:\n";


// Display details of each student
for (int i = 0; i < n; ++i) {
cout << "Details for student " << i + 1 << ":" << endl;
students[i].display();
}
}

Output:

Enter the number of students: 2


Enter details for student 1:
Enter name: Ram
Enter roll number: 46
Enter grade: A
Enter details for student 2:
Enter name: Arun
Enter roll number: 47
Enter grade: B

Student Details:
Details for student 1:
Name: Ram, Roll No.: 46, Grade: A
Details for student 2:
Name: Arun, Roll No.: 47, Grade: B

You might also like