You are on page 1of 7

#include<iostream>

using namespace std;


class student
{
private:
string regNum, name;
int marks;
public:
void enter_student_details();
void display_student_details();
};
void student::enter_student_details()
{
cout<<"Enter the student registration number: ";
getline(cin,regNum);
cout<<"Enter the student full names: ";
getline(cin,name);
cout<<"Enter the student marks: ";
cin>>marks;
}
void student::display_student_details()
{
cout<<"Student registration number: "<<regNum<<endl;
cout<<"Student full names: "<<name<<endl;
cout<<"The student marks: "<<marks<<endl;
}
main()
{
student s;
s.enter_student_details();
s.display_student_details();
}

#include<iostream>
using namespace std;
struct student
{
string regNum, name;
int marks;
};
main()
{
student s;
s.regNum="SCT213-c002-0023/2023";
s.name="Carol Nyanjui" ;
s.marks=78;
}
#include<iostream>
using namespace std;
class student
{
private:
string regNum, name;
int marks;
public:
void enter_student_details();
void display_student_details();
}s1;
void student::enter_student_details()
{
cout<<"Enter the student registration number: ";
getline(cin,regNum);
cout<<"Enter the student full names: ";
getline(cin,name);
cout<<"Enter the student marks: ";
cin>>marks
}
void student::display_student_details()
{
cout<<"Student registration number: "<<regNum<<endl;
cout<<"Student full names: "<<name<<endl;
cout<<"The student marks: "<<marks<<endl;
}
main()
{
student s;
cout<<"Enter the details of the first student"<<endl;
cout<<"----------------------------------------"<<endl;
s.enter_student_details();
s.display_student_details();

cout<<"Enter the details of the second student"<<endl;


cout<<"------------------------------------------"<<endl;
s1.enter_student_details();
s1.display_student_details();
}
#include<iostream>
using namespace std;
main()
{
float length,width,area;
area=length*width;
cout<<"The length is: "<<length<<endl;
cout<<"The width is: "<<width<<endl;
cout<<"The area is: "<<area<<endl;
}

#include<iostream>
using namespace std;
main()
{
float length=0.0,width=0.0,area=0.0,peri=0.0;
cout<<"Computing the area and perimeter using the initial values"<<endl;
cout<<"----------------------------------------------------------------"<<endl;
area=length*width;
peri=2*(length+width);
cout<<"The length is: "<<length<<endl;
cout<<"The width is: "<<width<<endl;
cout<<"The area is: "<<area<<endl;
cout<<"The perimeter is: "<<peri<<endl;

cout<<"Computing the area and perimeter using the our own values"<<endl;
cout<<"-------------------------------------------------------------------"<<endl;
cout<<"Enter the length of the rectangle: ";
cin>>length;
cout<<"Enter the width of the rectangle: ";
cin>>width;
area=length*width;
peri=2*(length+width);
cout<<"The length is: "<<length<<endl;
cout<<"The width is: "<<width<<endl;
cout<<"The area is: "<<area<<endl;
cout<<"The perimeter is: "<<peri<<endl;
}
CONSTRUCTORS
A constructor is a 'special ' member function whose task is to automatically
initialize the objects of its class. It is special because its name is the same as
the class name. The constructor is invoked whenever an object of its associated
class is created. It does not have a return value not even void. It is called a
constructor because it constructs the values of data members of the class.
Characteristics
It does not have a return value not even void
It has the same name as a class
It can have parameters (parameterized constructors) or no parameters(default or
parameterized constructors)
They should be declared in the public section
They should be invoked automatically when the objects are created
They cannot be inherited, though a derived class can call the base class
constructor

data member or member variables


#include<iostream>
using namespace std;
class student
{
private:
string regNum, name;
int marks;
public:
void enter_student_details();
void display_student_details();
student(); //default or non-parameterized constructor
student(string, string, int); ////parameterized constructor
~student();
}s1;
student::student() //default or non-parameterized constructor
{
regNum="SCT213-C002-0001/2024";
name="George Morara";
marks=40;
}
student::student(string r, string n, int m)//parameterized constructor
{
regNum=r;
name=n;
marks=m;
}
student::~student()
{
cout<<"Objects have been destroyed"<<endl;
}
void student::enter_student_details()
{
cout<<"Enter the student registration number: ";
getline(cin,regNum);
cout<<"Enter the student full names: ";
getline(cin,name);
cout<<"Enter the student marks: ";
cin>>marks
}
void student::display_student_details()
{
cout<<"Student registration number: "<<regNum<<endl;
cout<<"Student full names: "<<name<<endl;
cout<<"The student marks: "<<marks<<endl;
}
main()
{
student s;
cout<<"Enter the details of the first student"<<endl;
cout<<"----------------------------------------"<<endl;
s.enter_student_details();
s.display_student_details();

cout<<"Enter the details of the second student"<<endl;


cout<<"------------------------------------------"<<endl;
s1.enter_student_details();
s1.display_student_details();
}

#include<iostream>
using namespace std;
class student
{
private:
string regNum, name;
int marks;
public:
void enter_student_details();
void display_student_details();
student(); //default or non-parameterized constructor
student(string, string, int); ////parameterized constructor
~student();
}s1;
student::student() //default or non-parameterized constructor
{
regNum="SCT213-C002-0001/2024";
name="George Morara";
marks=40;
}
student::student(string r, string n, int m)//parameterized constructor
{
regNum=r;
name=n;
marks=m;
}
student::~student()
{
cout<<"Objects have been destroyed"<<endl;
}
void student::enter_student_details()
{
cout<<"Enter the student registration number: ";
getline(cin,regNum);
cout<<"Enter the student full names: ";
getline(cin,name);
cout<<"Enter the student marks: ";
cin>>marks;
}
void student::display_student_details()
{
cout<<"Student registration number: "<<regNum<<endl;
cout<<"Student full names: "<<name<<endl;
cout<<"The student marks: "<<marks<<endl;
}
main()
{
student s("SCT213-c002-0023/2023","Carol Nyanjui",45);
cout<<"Initilization using the parameterized constructors "<<endl;
cout<<"-----------------------------------------------------"<<endl;
s.display_student_details();

cout<<"Initilization using the non-parameterized constructors "<<endl;


cout<<"---------------------------------------------------------"<<endl;
s1.display_student_details();

cout<<"Enter the details of the first student"<<endl;


cout<<"----------------------------------------"<<endl;
s.enter_student_details();
s.display_student_details();

cout<<"Enter the details of the second student"<<endl;


cout<<"------------------------------------------"<<endl;
s1.enter_student_details();
s1.display_student_details();
}

Write an OOP program that stores the details of a product purchased by a customer
i.e the product code,quantity,unit price and product name.The program should be
able to store, display
and compute the total price of the product.Include a paramterized and non-
parameterized constructors to
initilize the product details. Also include the destructor to release the memory
spaces.
Required
1. Test your program by calculating the total price paid by the customer using the
two constructor
2. Test your program by calculating the total price using the values entered by the
user.

You might also like