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

Graded Lab The Hidden This Pointer, Static Data Member

The document introduces several C++ concepts including the this pointer, member initializer lists, const qualifiers, static members, and arrays of objects. It provides sample code to demonstrate each concept. The document also outlines a lab task to design a book inventory system using a Book class with functions to search for books, display book details, update prices, track stock levels, and count successful and unsuccessful transactions.

Uploaded by

alielina53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Graded Lab The Hidden This Pointer, Static Data Member

The document introduces several C++ concepts including the this pointer, member initializer lists, const qualifiers, static members, and arrays of objects. It provides sample code to demonstrate each concept. The document also outlines a lab task to design a book inventory system using a Book class with functions to search for books, display book details, update prices, track stock levels, and count successful and unsuccessful transactions.

Uploaded by

alielina53
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

The Hidden This Pointer, Member Initializer List, Const

Qualifier, The Use of keyword Static and Array of Objects

Objectives
The purpose of this activity is to introduce students with the concepts and use of this pointer, static member variable, its
use and const qualifier, use of key word static along with the creation of arrays of objects.

The hidden This Pointer


Sample Code 01:

If you have a constructor (or member function) that has a parameter of the same name as a member variable, you can
disambiguate them by using “this”:

class Something

private:

int nData;

public:

Something(int nData)

this->nData = nData;

};
Sample Code 02:
class Calc

private:

int m_nValue;

public:

Calc() { m_nValue = 0; }

void Add(int nValue) { m_nValue += nValue; } void Sub(int

nValue) { m_nValue -= nValue; } void Mult(int nValue)

{ m_nValue *= nValue; }

int GetValue() { return m_nValue; }

};

int main()

Calc cCalc;

cCalc.Add(5);

cCalc.Sub(3);

cCalc.Mult(4);

cout<<cCalc.GetValue()<<endl; return 0;

}
Sample Code 03:
class Calc

private:

int m_nValue;

public:

Calc() { m_nValue = 0; }

Calc& Add(int nValue) { m_nValue += nValue; return *this; }

Calc& Sub(int nValue) { m_nValue -= nValue; return *this; }

Calc& Mult(int nValue) { m_nValue *= nValue; return *this; }

int GetValue() { return m_nValue; }

};

int main()

Calc cCalc;

cCalc.Add(5).Sub(3).Mult(4);

cout<<cCalc.GetValue()<<endl;

return 0;

Member Initialization List


class Something
{
private:
const int m_nValue;
public:
Something(): m_nValue(5)
{

}
};
Const class objects and member Functions
class Date

private:

int m_nMonth;

int m_nDay;

int m_nYear;

Date() { }

public:

Date(int nMonth, int nDay, int nYear)

SetDate(nMonth, nDay, nYear);

void SetDate(int nMonth, int nDay, int nYear)

m_nMonth = nMonth; m_nDay = nDay; m_nYear = nYear;

int GetMonth() const { return m_nMonth; }


int GetDay() const { return m_nDay; }

int GetYear() const { return m_nYear; }

};

void PrintDate(const Date &cDate)

std::cout << cDate.GetMonth() << "/" << cDate.GetDay() << "/" << cDate.GetYear() << std::endl;
}

int main()

const Date cDate(10, 16, 2020);


PrintDate(cDate);

return 0;

Static Member Variable


class Something

private:

static int s_nIDGenerator; int m_nID;

public:

Something() { m_nID = s_nIDGenerator++; }

int GetID() const { return m_nID; }

};

int Something::s_nIDGenerator = 1;

int main()

Something cFirst;

Something cSecond;

Something cThird;

cout << cFirst.GetID()<<endl; cout << cSecond.GetID()<<endl;

cout << cThird.GetID()<<endl;

return 0;

}
Static Member Function
class IDGenerator

private:

static int s_nNextID;

public:

static int GetNextID()

return s_nNextID++;

};

int IDGenerator::s_nNextID = 1;

int main()

for (int i=0; i < 5; i++)

cout << "The next ID is: " << IDGenerator::GetNextID() << endl;

return 0;

}
Array of Objects
Sample Code 01: Create an array of objects

#include <iostream>

using namespace std;

class MyClass {

int x;

public:
void setX(int i) { x = i; }

int getX() { return x; }


};

int main()

{
MyClass bs[4];

int i;

for(i=0; i < 4; i++)

obs[i].setX(i);

for(i=0; i < 4; i++)

cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n";

return 0;
}
Sample Code 02: An array of objects: call its method
#include <iostream>

class MyClass

public:

MyClass() {

itsAge = 1;

itsWeight=5;
}

~MyClass() {}

int GetAge() const {

return itsAge;
}

int GetWeight() const {

return itsWeight;
}

void SetAge(int age) {

itsAge = age;
}

private:

int itsAge;

int itsWeight;
};

int main()
{
MyClass myObject[5];
int i;
for (i = 0; i < 5; i++)
myObject[i].SetAge(2*i +1);

for (i = 0; i < 5; i++)


std::cout << " #" << i+1<< ": " << myObject[i].GetAge() << std::endl;

return 0;
}
Sample Code 03: Initialize an array of objects without referencing the constructor directly

#include <iostream> using


namespace std;

class MyClass { int


x;
public:
MyClass(int i) { x = i; } int
getX() { return x; }
};

int main()
{
MyClass obs[4] = { -1, -2, -3, -4 }; int i;

for(i=0; i < 4; i++)


cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n";

return 0;
}

Sample Code 04: Initialize an array of objects by referencing the constructor directly

#include <iostream>
using namespace std;

class MyClass {
int x;
public:
MyClass(int i) { x = i; }
int getX() { return x; }
};

int main()
{
MyClass obs[4] = { MyClass(-1), MyClass (-2), MyClass (-3), MyClass (-4) };
int i;

for(i=0; i < 4; i++)


cout << "obs[" << i << "].getX(): " << obs[i].getX() << "\n";

return 0;
}
Lab Tasks
Task 1:
A book shop maintains the inventory of books that are being sold at the shop. The list includes details
such as author, title, price, publisher and stock position. Whenever a customer wants a book, the sales person
inputs the title and author and the system searches the list and displays weather it available or not. If it is not,
an appropriate message is displayed. If it is, then the system displays the book details and request for the
number of copies required. If the requested copies are available, the total cost of requested copies is displayed;
otherwise the message “Required copies not in the stock” is displayed. Design a system using a class called
books with suitable member functions and constructors. Use new operator in constructors to allocate memory
space if required. Provide these extra facilities in the program:

1. The price of the books should be updated as and when required. Use a private member function to
implement this.

2. The stock value of each book should be automatically updated as soon as a transaction is
completed.

3. The number of successful and unsuccessful transactions should be recorded for the purpose of
statistical analysis. Use static data members to keep count of transactions.

#include<iostream>
using namespace std;

class book
{
string author;
string title;
string publisher;
float price;
int stock;
static int succ_trans;
static int unsucc_trans;

public:
book(string author,string title,string publisher,float price,int stock)
{
this->author=author;
this->title=title;
this->publisher=publisher;
this->price=price;
this->stock=stock;

void display()
{
cout<<"author:"<<author<<endl;
cout<<"title :"<<title<<endl;
cout<<"publisher : "<<publisher<<endl;
cout<<"price of book "<<price<<endl;
cout<<"stock: "<<stock<<endl;

}
void update(float newprice)
{
price=newprice;
}

void transaction(int requestedcopy)


{
int total_cost=0;
if(requestedcopy<=stock)
{
cout<<"it is in stock "<<endl;
cout<<"price before update "<<stock<<endl;
cout<<"insert your new updated price "<<endl;
cin>>newprice;

//making the transactions detail


total_cost=requestedcopy*price;
cout<<"your transaction is successful"<<total_cost<<endl;
stock=stock-requestedcopy;
succ_trans++;

}
else
{
cout<<"out of stock";
unsucc_trans++;

}
}
static int getsucc_tans()
{
return succ_trans;
}
static int getunsucc_trans()
{
return unsucc_trans;
}

};
int main()
{
book b("fredy cruger","thousand splendid suns","san diego",35.5,30);
b.display();
int requestedcoy;
cout<<"enter your requested copies "<,endl;
cin>>requestedcopy;
b.transaction(requestedcopy);
b.getsucc_trans();
b.getunsucc_trans();
cout<<"no. of successful transactions"<<getsucc_trans<<endl;
cout<<"no. of unsuccessful transactions"<<getunsucc_trans<<endl;

You might also like