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

CS304 Lecture 9 Programs

The document contains three tasks related to Object-Oriented Programming (OOP) in C++. Task 1 demonstrates the use of a constructor and destructor in a Student class, Task 2 introduces accessor functions for setting and getting student ID and name, and Task 3 illustrates the use of the 'this' pointer for member variable assignment. Each task includes code snippets and basic validation for input values.

Uploaded by

danirajput422
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

CS304 Lecture 9 Programs

The document contains three tasks related to Object-Oriented Programming (OOP) in C++. Task 1 demonstrates the use of a constructor and destructor in a Student class, Task 2 introduces accessor functions for setting and getting student ID and name, and Task 3 illustrates the use of the 'this' pointer for member variable assignment. Each task includes code snippets and basic validation for input values.

Uploaded by

danirajput422
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Lecture No.

9 OOP Programs

// Task 1: Desctructor in the Class


#include<iostream>
#include<conio.h>
using namespace std;
class Student
{
public:
// Constructor
Student()
{}
// Destructor
~Student()
{}
};
main()
{

getch();
return 0;
}

// Task 2: Accessor Functions


#include<iostream>
#include<conio.h>
using namespace std;
class Student
{
private:
int id;
string name;
public:
void setID(int i)
{
if(i <= 0)
{
cout<<"\n\n Invalid Roll No.";
}
else
{
id = i;
}
}
int getID()
{
return id;
}
void setName(string n)
{
if(n == "")
{
cout<<"\n\n Invalid Name";
}
else
{
name = n;
}
}
string getName()
{
return name;
}
};
main()
{
Student s;
s.setID(100);
s.setName("Ali");
cout<<s.getID();
cout<<"\t"<<s.getName();
getch();
return 0;
}

// Task 3: this Pointer


#include<iostream>
#include<conio.h>
using namespace std;
class Student
{
private:
int id;
string name;
public:
void setID(int id)
{
if(id <= 0)
{
cout<<"\n\n Invalid Roll No.";
}
else
{
this -> id = id;
}
}
int getID()
{
return id;
}
void setName(string name)
{
if(name == "")
{
cout<<"\n\n Invalid Name";
}
else
{
this -> name = name;
}
}
string getName()
{
return name;
}
};
main()
{
Student s;
s.setID(100);
s.setName("Ali");
cout<<s.getID();
cout<<"\t"<<s.getName();
getch();
return 0;
}

You might also like