SlideShare a Scribd company logo
1 of 14
CONSTRUCTORS
A constructor in C++ is a special ‘MEMBER FUNCTION’ having the same name as that of its
class which is used to initialize some valid values to the data members of an object. It is executed
automatically whenever an object of a class is created. The only restriction that applies to the
constructor is that it must not have a return type or void. It is because the constructor is
automatically called by the compiler and it is normally used to INITIALIZE VALUES. The
compiler distinguishes the constructor from other member functions of a class by its name which
is the same as that of its class. ctorz is an abbreviation of constructor in C+
C++ constructor can be defined as a class in the same way as that of normal member functions
and can access any of its data members. The syntax for defining constructor inside the class body
is as follows:
class CLASSNAME
{
………
public :
CLASSNAME([parameter_list]) // constructor definition
{
. . . . . .
}
. . . . . . . .
};
INSIDETHECLASS
class CLASSNAME
{
. . . . . . . .
public:
CLASSNAME ([parameter_list]);//Constructor declaration
. . . . . . . . .
};
CLASSNAME: :CLASSNAME([parameter_list])//Constructor Definition
{
. . . . . . . . . . .
}
OUTSIDETHECLASS
class Wall
 public:
// create a constructor
Wall() {
// code
}
};
Here, the function Wall() is a constructor of the class Wall. Notice that the constructor
has the same name as the class,
does not have a return type, and
is public
A constructor to which no arguments are passed is called the Default
constructor. It is also called a constructor with no parameters.
In the example above, Wall() is a default constructor.
Using the default constructor, data members can be initialized to
some realistic values in its definition even though no arguments are
specified explicitly.
C++DEFAULTCONSTRUCTOR
EXAMPLE 1
1. If we have not defined a
constructor in our class,
then the C++ compiler
will automatically create
a default constructor with
an empty code and no
parameters.
Initialize a student’s data ( university name,
batch, roll no) using a default constructor.
Use string to get university name;
TASK 1
In C++, a constructor with parameters is known as a parameterized constructor. This is the
preferred method to initialize member data.

C++PARAMETERIZEDCONSTRUCTOR
EXAMPLE 2
EXAMPLE 3
Using parameterized constructor, initialize a
person’s bank account data ( Account title,
Account number, Account total salary ). Hint:
Use string to get the account title.
TASK 2
1. Using parameterized constructor, initialize the account salary
of a person in may , june and july. Using functions calculate
total salary. If the salary for each of these month is increased 30
percent, display the new total salary for these months
2. Write a program that defines a constructor to find the
maximum and minimum values for 3 numbers
HOME TASK

More Related Content

Similar to Lab 6- Constructors in c++ . IST .pptx

Constructor& destructor
Constructor& destructorConstructor& destructor
Constructor& destructorchauhankapil
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#ANURAG SINGH
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop Samad Qazi
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects newlykado0dles
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructorrajshreemuthiah
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCEVenugopalavarma Raja
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptxEpsiba1
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdfstudy material
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructorDa Mystic Sadi
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Asfand Hassan
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cppgourav kottawar
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptxRassjb
 

Similar to Lab 6- Constructors in c++ . IST .pptx (20)

Constructor& destructor
Constructor& destructorConstructor& destructor
Constructor& destructor
 
Oops concept in c#
Oops concept in c#Oops concept in c#
Oops concept in c#
 
Constructor and destructor in oop
Constructor and destructor in oop Constructor and destructor in oop
Constructor and destructor in oop
 
Classes & objects new
Classes & objects newClasses & objects new
Classes & objects new
 
Constructor and destructor
Constructor and destructorConstructor and destructor
Constructor and destructor
 
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCECONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
CONSTRUCTORS IN C++ +2 COMPUTER SCIENCE
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
chapter-9-constructors.pdf
chapter-9-constructors.pdfchapter-9-constructors.pdf
chapter-9-constructors.pdf
 
C++ Constructor destructor
C++ Constructor destructorC++ Constructor destructor
C++ Constructor destructor
 
Constructor
ConstructorConstructor
Constructor
 
OOPs & C++ UNIT 3
OOPs & C++ UNIT 3OOPs & C++ UNIT 3
OOPs & C++ UNIT 3
 
Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)Oop lec 5-(class objects, constructor & destructor)
Oop lec 5-(class objects, constructor & destructor)
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
constructor & destructor in cpp
constructor & destructor in cppconstructor & destructor in cpp
constructor & destructor in cpp
 
Structures
StructuresStructures
Structures
 
Constructors in C++.pptx
Constructors in C++.pptxConstructors in C++.pptx
Constructors in C++.pptx
 
OOC MODULE1.pptx
OOC MODULE1.pptxOOC MODULE1.pptx
OOC MODULE1.pptx
 
Objects and Types C#
Objects and Types C#Objects and Types C#
Objects and Types C#
 
C++ language
C++ languageC++ language
C++ language
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 

More from saadpisjes

Safety_Health_and_Environment .IST .pptx
Safety_Health_and_Environment .IST .pptxSafety_Health_and_Environment .IST .pptx
Safety_Health_and_Environment .IST .pptxsaadpisjes
 
Safety_Health_and_Environment . ist .pptx
Safety_Health_and_Environment . ist .pptxSafety_Health_and_Environment . ist .pptx
Safety_Health_and_Environment . ist .pptxsaadpisjes
 
Sorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .pptSorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .pptsaadpisjes
 
Concept of Classes in c++.ist mse16.pptx
Concept of Classes in c++.ist mse16.pptxConcept of Classes in c++.ist mse16.pptx
Concept of Classes in c++.ist mse16.pptxsaadpisjes
 
Administrative Structure of Pakistan IST.pptx
Administrative Structure of Pakistan  IST.pptxAdministrative Structure of Pakistan  IST.pptx
Administrative Structure of Pakistan IST.pptxsaadpisjes
 
Administrative Structure of Pakistan IST.pptx
Administrative Structure of Pakistan  IST.pptxAdministrative Structure of Pakistan  IST.pptx
Administrative Structure of Pakistan IST.pptxsaadpisjes
 
Geo-Strategic Significance of Pakistan.pptx IST.pptx
Geo-Strategic Significance of Pakistan.pptx IST.pptxGeo-Strategic Significance of Pakistan.pptx IST.pptx
Geo-Strategic Significance of Pakistan.pptx IST.pptxsaadpisjes
 

More from saadpisjes (7)

Safety_Health_and_Environment .IST .pptx
Safety_Health_and_Environment .IST .pptxSafety_Health_and_Environment .IST .pptx
Safety_Health_and_Environment .IST .pptx
 
Safety_Health_and_Environment . ist .pptx
Safety_Health_and_Environment . ist .pptxSafety_Health_and_Environment . ist .pptx
Safety_Health_and_Environment . ist .pptx
 
Sorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .pptSorting of arrays, types in c++ .IST .ppt
Sorting of arrays, types in c++ .IST .ppt
 
Concept of Classes in c++.ist mse16.pptx
Concept of Classes in c++.ist mse16.pptxConcept of Classes in c++.ist mse16.pptx
Concept of Classes in c++.ist mse16.pptx
 
Administrative Structure of Pakistan IST.pptx
Administrative Structure of Pakistan  IST.pptxAdministrative Structure of Pakistan  IST.pptx
Administrative Structure of Pakistan IST.pptx
 
Administrative Structure of Pakistan IST.pptx
Administrative Structure of Pakistan  IST.pptxAdministrative Structure of Pakistan  IST.pptx
Administrative Structure of Pakistan IST.pptx
 
Geo-Strategic Significance of Pakistan.pptx IST.pptx
Geo-Strategic Significance of Pakistan.pptx IST.pptxGeo-Strategic Significance of Pakistan.pptx IST.pptx
Geo-Strategic Significance of Pakistan.pptx IST.pptx
 

Recently uploaded

Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatmentsaipooja36
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024CapitolTechU
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Celine George
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45MysoreMuleSoftMeetup
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project researchCaitlinCummins3
 
Financial Accounting IFRS, 3rd Edition-dikompresi.pdf
Financial Accounting IFRS, 3rd Edition-dikompresi.pdfFinancial Accounting IFRS, 3rd Edition-dikompresi.pdf
Financial Accounting IFRS, 3rd Edition-dikompresi.pdfMinawBelay
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...Nguyen Thanh Tu Collection
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppCeline George
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Mohamed Rizk Khodair
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Celine George
 
HVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptx
HVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptxHVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptx
HVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptxKunal10679
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxCeline George
 
....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdfVikramadityaRaj
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Celine George
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the lifeNitinDeodare
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptxmanishaJyala2
 

Recently uploaded (20)

Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
Envelope of Discrepancy in Orthodontics: Enhancing Precision in Treatment
 
Word Stress rules esl .pptx
Word Stress rules esl               .pptxWord Stress rules esl               .pptx
Word Stress rules esl .pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024Capitol Tech Univ Doctoral Presentation -May 2024
Capitol Tech Univ Doctoral Presentation -May 2024
 
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
Removal Strategy _ FEFO _ Working with Perishable Products in Odoo 17
 
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
Exploring Gemini AI and Integration with MuleSoft | MuleSoft Mysore Meetup #45
 
SURVEY I created for uni project research
SURVEY I created for uni project researchSURVEY I created for uni project research
SURVEY I created for uni project research
 
Financial Accounting IFRS, 3rd Edition-dikompresi.pdf
Financial Accounting IFRS, 3rd Edition-dikompresi.pdfFinancial Accounting IFRS, 3rd Edition-dikompresi.pdf
Financial Accounting IFRS, 3rd Edition-dikompresi.pdf
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT VẬT LÝ 2024 - TỪ CÁC TRƯỜNG, TRƯ...
 
An Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge AppAn Overview of the Odoo 17 Knowledge App
An Overview of the Odoo 17 Knowledge App
 
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
Operations Management - Book1.p  - Dr. Abdulfatah A. SalemOperations Management - Book1.p  - Dr. Abdulfatah A. Salem
Operations Management - Book1.p - Dr. Abdulfatah A. Salem
 
Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).Dementia (Alzheimer & vasular dementia).
Dementia (Alzheimer & vasular dementia).
 
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 2 STEPS Using Odoo 17
 
HVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptx
HVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptxHVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptx
HVAC System | Audit of HVAC System | Audit and regulatory Comploance.pptx
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
An Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptxAn Overview of the Odoo 17 Discuss App.pptx
An Overview of the Odoo 17 Discuss App.pptx
 
....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf....................Muslim-Law notes.pdf
....................Muslim-Law notes.pdf
 
Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17Features of Video Calls in the Discuss Module in Odoo 17
Features of Video Calls in the Discuss Module in Odoo 17
 
philosophy and it's principles based on the life
philosophy and it's principles based on the lifephilosophy and it's principles based on the life
philosophy and it's principles based on the life
 
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptxREPRODUCTIVE TOXICITY  STUDIE OF MALE AND FEMALEpptx
REPRODUCTIVE TOXICITY STUDIE OF MALE AND FEMALEpptx
 

Lab 6- Constructors in c++ . IST .pptx

  • 2. A constructor in C++ is a special ‘MEMBER FUNCTION’ having the same name as that of its class which is used to initialize some valid values to the data members of an object. It is executed automatically whenever an object of a class is created. The only restriction that applies to the constructor is that it must not have a return type or void. It is because the constructor is automatically called by the compiler and it is normally used to INITIALIZE VALUES. The compiler distinguishes the constructor from other member functions of a class by its name which is the same as that of its class. ctorz is an abbreviation of constructor in C+ C++ constructor can be defined as a class in the same way as that of normal member functions and can access any of its data members. The syntax for defining constructor inside the class body is as follows:
  • 3. class CLASSNAME { ……… public : CLASSNAME([parameter_list]) // constructor definition { . . . . . . } . . . . . . . . }; INSIDETHECLASS
  • 4. class CLASSNAME { . . . . . . . . public: CLASSNAME ([parameter_list]);//Constructor declaration . . . . . . . . . }; CLASSNAME: :CLASSNAME([parameter_list])//Constructor Definition { . . . . . . . . . . . } OUTSIDETHECLASS
  • 5. class Wall  public: // create a constructor Wall() { // code } };
  • 6. Here, the function Wall() is a constructor of the class Wall. Notice that the constructor has the same name as the class, does not have a return type, and is public
  • 7. A constructor to which no arguments are passed is called the Default constructor. It is also called a constructor with no parameters. In the example above, Wall() is a default constructor. Using the default constructor, data members can be initialized to some realistic values in its definition even though no arguments are specified explicitly. C++DEFAULTCONSTRUCTOR
  • 8. EXAMPLE 1 1. If we have not defined a constructor in our class, then the C++ compiler will automatically create a default constructor with an empty code and no parameters.
  • 9. Initialize a student’s data ( university name, batch, roll no) using a default constructor. Use string to get university name; TASK 1
  • 10. In C++, a constructor with parameters is known as a parameterized constructor. This is the preferred method to initialize member data.  C++PARAMETERIZEDCONSTRUCTOR
  • 13. Using parameterized constructor, initialize a person’s bank account data ( Account title, Account number, Account total salary ). Hint: Use string to get the account title. TASK 2
  • 14. 1. Using parameterized constructor, initialize the account salary of a person in may , june and july. Using functions calculate total salary. If the salary for each of these month is increased 30 percent, display the new total salary for these months 2. Write a program that defines a constructor to find the maximum and minimum values for 3 numbers HOME TASK

Editor's Notes

  1. // C++ program to demonstrate the use of default constructor #include <iostream> using namespace std; // declare a class class Wall { private: double length; public: // create a constructor Wall() { // initialize private variables length = 5.5; cout << "Creating a wall." << endl; cout << "Length = " << length << endl; } }; int main() { // create an object Wall wall1; return 0; }
  2. #include<iostream> #include<string> using namespace std; class studentdata { private: string universityname; int Batch; char section; public: studentdata() { universityname= "IST"; Batch=13; section='A'; cout<<"The university name of this student is "<<universityname<<endl; cout<<"The Batch number of this student is "<<Batch<<endl; cout<<"The section of this student is " <<section; } }; int main() { studentdata s1; } //Method 2: #include<iostream> #include<string> using namespace std;   class studentdata { public: string universityname; int Batch; char section; studentdata() { universityname= "IST"; Batch=13; section='A'; } void printinitialdata() { cout<<"*********STUDENT's DATA FROM THE RECORD*******"<<endl; cout<<"The university name of this student is "<<universityname<<endl; cout<<"The Batch number of this student is "<<Batch<<endl; cout<<"The section of this student is " <<section; } }; int main() { studentdata s1; s1.printinitialdata(); }
  3. #include <iostream> using namespace std; // declare a class class Wall { private: double length; double height; public: // create parameterized constructor Wall(double len, double hgt) { // initialize private variables length = len; height = hgt; } double calculateArea() { return length * height; } }; int main() { // create object and initialize data members Wall wall1(10.5, 8.6); Wall wall2(8.5, 6.3); cout << "Area of Wall 1: " << wall1.calculateArea() << endl; cout << "Area of Wall 2: " << wall2.calculateArea() << endl; return 0; }
  4. #include<iostream> using namespace std; class marks { private: int physics; int chemistry; int bio; public: marks(int a, int b ,int c) { physics=a; chemistry=b; bio=c; printmarks(); } void newtotalmarks(int d , int e ,int f ) { physics=physics+d; chemistry=chemistry+e; bio=bio+f; printmarks(); } void printmarks() { cout<<"Total marks in physics are "<<physics<<endl; cout<<"Total marks in chemistry are "<<chemistry<<endl; cout<<"Total marks in bio are "<<bio<<endl; } }; int main() { cout<<"***** Total Marks of 3 subjects after exam 1*******"<<endl; marks s1( 23, 54 ,54 ); int a, b, c; cout<<"Enter the marks of exam 2 of physics"; cin>>a; cout<<"Enter the marks of exam 2 of chemistry"; cin>>b; cout<<"Enter the marks of exam 2 of bio"; cin>>c; cout<<"***** Total Marks of 3 subjects after exam 2*******"<<endl; s1.newtotalmarks(a, b, c); }
  5. #include<iostream> #include<string> using namespace std; class accountdata { private: string accounttitle; int accountnumber; int totalsalary; public: accountdata(string a , int b , int c) { accounttitle=a; accountnumber=b; totalsalary=c; displaydata(); } void displaydata() { cout<<"Account title is "<<accounttitle<<endl; cout<<"Account number is"<<accountnumber<<endl; cout<<"Total saray till now is "<<totalsalary<<endl; } }; int main() { accountdata a("IST",2,3); }