The document discusses C++ concepts like static data members, static member functions, static objects, constant data members, and constant member functions through examples. It includes code snippets demonstrating how to declare and use static data members and static member functions. It also shows examples of constant data members, constant member functions, and constant objects in C++.
2. This document will help you to understand the
concepts of C++ through simple programs.The
topics covered in this document are
Static Data Members
Static Member Functions
Static Objects
Constant Data Members
Constant Member Functions
Constant Objects
mohammed.sikander@cranessoftware.com 2
3. class TestStatic
{
int a;
static int s;
};
int main( )
{
cout << sizeof(TestStatic ) << endl;
}
mohammed.sikander@cranessoftware.com 3
4. class TestStatic
{
public : int a;
static int s;
};
int main( )
{
TestStatic obj;
obj.a = 5;
obj.s = 8;
cout << obj.a <<“ “ <<obj.s << endl;
}
mohammed.sikander@cranessoftware.com 4
5. class TestStatic
{
public : int a;
static int s;
};
int TestStatic::s ;
int main( )
{
TestStatic obj;
obj.a = 5;
obj.s = 8;
cout << obj.a <<“ “ <<obj.s << endl;
}
mohammed.sikander@cranessoftware.com 5
6. class TestStatic
{
public : int a;
static int s;
};
int TestStatic::s ;
mohammed.sikander@cranessoftware.com 6
int main( )
{
TestStatic sagar , maha;
sagar.a = 5 ; sagar.s = 5;
maha.a = 10 ; maha.s = 10;
cout <<sagar.a << sagar.s << endl;
cout << maha.a << maha.s << endl;
}
7. class Student {
public :
int m_regno;
string m_name;
static int count;
Student(string name) {
m_regno = ++count;
m_name = name;
}
void display( ) {
cout <<m_regno << m_name;
}
};
int Student::count = 0;
mohammed.sikander@cranessoftware.com 7
int main( )
{
Student s1(“CHETHAN”);
Student s2(“ATHIRA”);
Student *ptr;
ptr=new Student(“NAVEEN”);
s1.display( );
s2.display( );
ptr->display( );
}
8. class Student {
public :
int m_regno;
string m_name;
static int count;
//Appropriate constructors
are written
void display( ) {
cout << m_name << count;
}
static void print( ) {
cout << m_name << count;
}
};
int Student::count = 0;
mohammed.sikander@cranessoftware.com 8
int main( )
{
Student s1(“CHETHAN”);
Student s2(“ATHIRA”);
s1.display( );
s2.display( );
s1.print( );
s2.print( );
}
9. class Student {
public :
int m_regno;
string m_name;
static int count;
//Appropriate constructors
are written
void display( ) {
cout << m_name << count;
}
static void print( ) {
cout << count;
}
};
int Student::count = 0;
mohammed.sikander@cranessoftware.com 9
int main( )
{
Student s1(“CHETHAN”);
Student s2(“ATHIRA”);
s1.display( );
s2.display( );
Student::print( );
Student::print( );
}
10. class Student
{
Student obj;
};
Int main( )
{
cout << sizeof(Student);
}
mohammed.sikander@cranessoftware.com 10
class Student
{
static Student obj;
};
Int main( )
{
cout << sizeof(Student);
}
12. Can static objects invoke non-static
functions. Demonstrate through code
mohammed.sikander@cranessoftware.com 12
13. class Student{
int m_regno;
string m_name;
public :
Student(int regno ,string name) {
m_regno = regno;
m_name = name;
}
void display( ) {
cout <<“Non-static Function n”;
cout << m_regno <<“ “<<m_name << endl;
}
};
int main( ) {
static Student s(1 , “SIKANDER”);
s.display( );
}
mohammed.sikander@cranessoftware.com 13
14. class Test
{
int a ;
const int b;
public : Test( ) {
a = 0;
b = 0;
}
void print( ) {
cout <<a <<“t” << b << endl;
}
};
int main( )
{
Test t1;
t1.print( );
}
mohammed.sikander@cranessoftware.com 14
15. class Test
{
int a ;
const int b;
public : Test( ) : b(0)
{
a = 0;
}
void print( ) {
cout <<a <<“t” << b << endl;
}
};
int main( )
{
Test t1;
t1.print( );
}
mohammed.sikander@cranessoftware.com 15
16. class Test
{
int a ;
const int b;
public : Test(int x , int y ) : b(y){
a = x;
}
void print( ) const ;
};
void Test::print( )
{
cout <<a <<“t” << b << endl;
}
int main( )
{
Test t1 = Test(5 , 10);
t1.print( );
}
mohammed.sikander@cranessoftware.com 16
17. class MyStack
{
public :
int *buffer;
int top;
const int SIZE ;
public :
MyStack(int sz = 5)
{
top = -1;
SIZE = sz;
buffer = new int[sz];
}
};
Sikander 17
int main( )
{
MyStack s1 = MyStack(10);
cout << sizeof(s1);
}
18. 1. classTest {
2. int a ;
3. const int b;
4. public :Test(int x , int y ) : b(y){
5. a = x;
6. }
7. void print( ) const {
8. ++a;
9. ++b;
10. }
11. void display( ) {
12. ++a;
13. ++b;
14. }
15. };
mohammed.sikander@cranessoftware.com 18
1. int main( )
2. {
3. Test t1 =Test(5 , 10);
4. t1.print( );
5. t1.display( );
6. }
19. 1. classTest {
2. int a ;
3. const int b;
4. public :Test(int x , int y ) : b(y){
5. a = x;
6. }
7. void print( ) const {
8. ++a;
9. ++b;
10. }
11. void display( ) {
12. ++a;
13. ++b;
14. }
15. };
mohammed.sikander@cranessoftware.com 19
1. int main( )
2. {
3. Test t1 =Test(5 , 10);
4. t1.print( );
5. t1.display( );
6. }
20. 1. classTest
2. {
3. int a ;
4. const int b;
5. public :Test(int x , int y ) : b(y) {
6. a = x;
7. }
8. void print( ) const {
9. cout <<a << b << endl;
10. }
11. void display( ) {
12. cout <<a << b << endl;
13. }
14. };
15. int main( )
16. {
17. const Test t1 =Test(5 , 10);
18. t1.print( );
19. t1.display();
20. }
mohammed.sikander@cranessoftware.com 20
21. 1. classTest
2. {
3. int a ;
4. const int b;
5. public :Test(int x , int y ) : b(y) {
6. a = x;
7. }
8. void funa( ) const {
9. }
10. void funb( ) {
11. }
12. };
mohammed.sikander@cranessoftware.com 21
1. Data member a can be modified in funa;
2. Data member a can be modified in funb;
3. Data member b can be modified in funa;
4. Data member b can be modified in funb;