SlideShare uma empresa Scribd logo
1 de 23
Baixar para ler offline
Prepared by
Mohammed Sikander
Technical Lead
Cranes Software International Limited
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
class TestStatic
{
int a;
static int s;
};
int main( )
{
cout << sizeof(TestStatic ) << endl;
}
mohammed.sikander@cranessoftware.com 3
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
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
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;
}
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( );
}
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( );
}
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( );
}
class Student
{
Student obj;
};
Int main( )
{
cout << sizeof(Student);
}
mohammed.sikander@cranessoftware.com 10
class Student
{
static Student obj;
};
Int main( )
{
cout << sizeof(Student);
}
mohammed.sikander@cranessoftware.com 11
class Student
{
static Student obj;
Student( ) { }
public :
static Student *getInstance( )
{
return &obj;
}
void display( )
{
cout <<“Display Function”;
}
};
Student Student::obj;
int main( )
{
Student s1;
Student *ptr;
ptr = Student::getInstance( );
ptr->display( );
}
 Can static objects invoke non-static
functions. Demonstrate through code
mohammed.sikander@cranessoftware.com 12
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
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
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
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
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);
}
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. }
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. }
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
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;
mohammed.sikander@cranessoftware.com 22
classA
{
public :
A( ) {
cout <<"Default Constructor " << endl;
}
A(int x){
cout <<"A Para Constructor "<<endl;
}
~A( ){
cout <<"A Destructor "<<endl;
}
void operator = (constA &x){
cout <<"A = operator" << endl;
}
};
mohammed.sikander@cranessoftware.com 23
classTest
{
A a ;
public :Test( )
{
a = 0;
}
};
int main( )
{
Test t1;
}

Mais conteúdo relacionado

Mais procurados

Travel management
Travel managementTravel management
Travel management
1Parimal2
 

Mais procurados (20)

Function basics
Function basicsFunction basics
Function basics
 
Implementing string
Implementing stringImplementing string
Implementing string
 
C++ Question on References and Function Overloading
C++ Question on References and Function OverloadingC++ Question on References and Function Overloading
C++ Question on References and Function Overloading
 
C++ Programming - 1st Study
C++ Programming - 1st StudyC++ Programming - 1st Study
C++ Programming - 1st Study
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
C questions
C questionsC questions
C questions
 
C++ file
C++ fileC++ file
C++ file
 
C programs
C programsC programs
C programs
 
C++ programs
C++ programsC++ programs
C++ programs
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Arrays
ArraysArrays
Arrays
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
C sharp 8
C sharp 8C sharp 8
C sharp 8
 
C program
C programC program
C program
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
C++ TUTORIAL 1
C++ TUTORIAL 1C++ TUTORIAL 1
C++ TUTORIAL 1
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
Travel management
Travel managementTravel management
Travel management
 

Semelhante a Static and const members

Semelhante a Static and const members (20)

L10
L10L10
L10
 
Oop objects_classes
Oop objects_classesOop objects_classes
Oop objects_classes
 
Online examination
Online examinationOnline examination
Online examination
 
What's new in C# 6 - NetPonto Porto 20160116
What's new in C# 6  - NetPonto Porto 20160116What's new in C# 6  - NetPonto Porto 20160116
What's new in C# 6 - NetPonto Porto 20160116
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
39927902 c-labmanual
39927902 c-labmanual39927902 c-labmanual
39927902 c-labmanual
 
Hems
HemsHems
Hems
 
Oop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer MelayiOop concept in c++ by MUhammed Thanveer Melayi
Oop concept in c++ by MUhammed Thanveer Melayi
 
[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projects
 
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
Kotlin Developer Starter in Android - STX Next Lightning Talks - Feb 12, 2016
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continued
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdf
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual final
 
Java oops features
Java oops featuresJava oops features
Java oops features
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_paper
 
C++ practical
C++ practicalC++ practical
C++ practical
 
JAVAPGMS.docx
JAVAPGMS.docxJAVAPGMS.docx
JAVAPGMS.docx
 

Último

1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
ZurliaSoop
 

Último (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Magic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptxMagic bus Group work1and 2 (Team 3).pptx
Magic bus Group work1and 2 (Team 3).pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Third Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptxThird Battle of Panipat detailed notes.pptx
Third Battle of Panipat detailed notes.pptx
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 

Static and const members

  • 1. Prepared by Mohammed Sikander Technical Lead Cranes Software International Limited
  • 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); }
  • 11. mohammed.sikander@cranessoftware.com 11 class Student { static Student obj; Student( ) { } public : static Student *getInstance( ) { return &obj; } void display( ) { cout <<“Display Function”; } }; Student Student::obj; int main( ) { Student s1; Student *ptr; ptr = Student::getInstance( ); ptr->display( ); }
  • 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;
  • 23. classA { public : A( ) { cout <<"Default Constructor " << endl; } A(int x){ cout <<"A Para Constructor "<<endl; } ~A( ){ cout <<"A Destructor "<<endl; } void operator = (constA &x){ cout <<"A = operator" << endl; } }; mohammed.sikander@cranessoftware.com 23 classTest { A a ; public :Test( ) { a = 0; } }; int main( ) { Test t1; }