Nothing Special   »   [go: up one dir, main page]

Lecture 7

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 18

CS-212

OBJECT ORIENTED
PROGRAMMING
MEHREEN TAHIR
Quiz 3
#include <iostream>
void populate_array(int *array, int N){
for (int i=0;i<N;i++){
std::cin>>array[i];
}
}
void print_array(int *array, int N){
for (int i=0;i<N;i++){
std::cout<<array[i]<<std::endl;
}
}
int main() {
int *a=new int[5];
populate_array(a,5);
print_array(a,5);
}
#include <iostream>
class Box{
private:
int l,w,h;
int *a;
public:
Box()
{l=w=h=0;
a=new int[2];
*a=5;
*(a+1)=8;}
Box(Box &b)
{l=b.l;h=b.h;w=b.w;
// a=b.a;
a=new int[2];
*a=*(b.a);
*(a+1)=*((b.a)+1);}
void setarr(int x,int y){
*a=x;
*(a+1)=y;}
void getarr(){
std::cout<<*a;
std::cout<<*(a+1);}
};
int main()
{ Box b;
Box x(b);
//Box z=b;
// z.getarr();
b.setarr(9,19);
b.getarr();
x.getarr();
}
Common Data
• What to do if you want to have some data
common or shared across ALL objects in a
class?
• For example, total number of students
Scope of Variables
• Life time of a variable in a program
• Local variables
• Global variables
Static members
• To solve this issue we can create a static
variable and give it an initial value.
• The initialization is performed only the first
time the function is called ,and the data
retains its value between function calls. This
way, a function can “remember” some piece
of information between function calls.
Static members
• No matter how many objects of the class are
created, there is only one copy of the static
member
• A static member is shared by all objects of the
class
Scope of static members
• Class scope
• Lifetime: entire program
• It exists without object
Static functions
• Static member functions can only access static
data members
• Non-static methods can access both static and
non-static data members.
Static members
• Static class variables (or data members)
• “Class-wide” data
• Property of class, not specific to object of class
• Efficient when single copy of data is enough
• It mainly represents the shared data
• Only the static variable has to be updated
• Only accessible to objects of same class
Static members
• Initialized exactly once in the class
Implementation file
• Exist even if no objects of class exist
• Lifespan: program START to program END
• Can be public , private or protected
const
• C++ introduces the concept of a named
constant that is just like a variable , except
that its value cannot be changed.
• const data members must be initialized using
member initializer list
const Member function
• A const member function guarantees that it
will never modify any of its data.
• Candidates for being made const because
they don’t need to modify any data.
• Member functions that do nothing but acquire
data from an object are obvious candidates
for being made const because they don’t need
to modify any data.
const Member function
• Any const member function that attempts to
change a member variable or call a non-const
member function will cause a compiler error
to occur.
• Constructors and destructors cannot be const
Mutable Data
• With const object you want to alter some data
Mutable Data
• The keyword mutable is mainly used to allow
a particular data member of const object to
be modified.
• Adding mutable to a variable allows a const
pointer to change members.
• mutable is particularly useful if most of the
members should be constant but a few need
to be updatable.
Mutable Data
• Data members declared as mutable can be
modified even though they are the part of
object declared as const.
• You cannot use the mutable specifier with
names declared as static or const, or
reference.
Mutable Data
#include <iostream>
class Mutable
{
public:
Mutable(){
val=0;
val1=0;
}
public:
mutable int val;
int val1;
};
int main() {
const Mutable m;
m.val=9;
// m.val1=6; // This line will throw an error because const objects cannot be modified. But mutable forces the object to modify itself
std::cout<< m.val;
return 0;
}

You might also like