SlideShare a Scribd company logo
1 of 23
Download to read 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;
}

More Related Content

What's hot (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
 

Similar to C++ Static Members and Constant Objects Guide

Online examination
Online examinationOnline examination
Online examinationAstha Patel
 
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 20160116Paulo Morgado
 
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 MelayiMuhammed Thanveer M
 
[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancji[PL] O klasycznej, programistycznej elegancji
[PL] O klasycznej, programistycznej elegancjiJakub Marchwicki
 
Kotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsKotlin Developer Starter in Android projects
Kotlin Developer Starter in Android projectsBartosz Kosarzycki
 
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, 2016STX Next
 
Session 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedSession 08 - OOP with Java - continued
Session 08 - OOP with Java - continuedPawanMM
 
ParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfParallelProgrammingBasics_v2.pdf
ParallelProgrammingBasics_v2.pdfChen-Hung Hu
 
Pads lab manual final
Pads lab manual finalPads lab manual final
Pads lab manual finalAhalyaR
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Kotlin Perfomance on Android / Александр Смирнов (Splyt)
Kotlin Perfomance on Android / Александр Смирнов (Splyt)Ontico
 
2014 computer science_question_paper
2014 computer science_question_paper2014 computer science_question_paper
2014 computer science_question_papervandna123
 

Similar to C++ Static Members and Constant Objects Guide (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
 

Recently uploaded

ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxsqpmdrvczh
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomnelietumpap1
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationAadityaSharma884161
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 

Recently uploaded (20)

ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
Romantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptxRomantic Opera MUSIC FOR GRADE NINE pptx
Romantic Opera MUSIC FOR GRADE NINE pptx
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
ENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choomENGLISH6-Q4-W3.pptxqurter our high choom
ENGLISH6-Q4-W3.pptxqurter our high choom
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
ROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint PresentationROOT CAUSE ANALYSIS PowerPoint Presentation
ROOT CAUSE ANALYSIS PowerPoint Presentation
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 

C++ Static Members and Constant Objects Guide

  • 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; }