0% found this document useful (0 votes)
4 views

Assignment 2 C++

The document contains a series of C++ programs written by Chinmay Chandrakant Terdale, showcasing various programming concepts such as classes, access modifiers, constructors, destructors, and friend functions. Each program includes code snippets, output examples, and comments on the execution process. The programs are structured to demonstrate specific programming principles in a clear and educational manner.

Uploaded by

uiuxnihal
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Assignment 2 C++

The document contains a series of C++ programs written by Chinmay Chandrakant Terdale, showcasing various programming concepts such as classes, access modifiers, constructors, destructors, and friend functions. Each program includes code snippets, output examples, and comments on the execution process. The programs are structured to demonstrate specific programming principles in a clear and educational manner.

Uploaded by

uiuxnihal
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 18

-------------------------------------------------------------------------------------------------------

Program No. :01


Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to display student information using class.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class Student{
public:
int Id;
string Name,College,Class;

void Insert(int i,string n,string c,string r){


Id=i;
Name=n;
College=c;
Class=r;
}
void display(){
cout<<"Student Id:"<<Id<<endl;
cout<<"Student Name:"<<Name<<endl;
cout<<"Student College:"<<College<<endl;
cout<<"Student Class:"<<Class;
}
};
int main(){
Student S;
S.Insert(01,"Chinmay","JCJ","BCA-II");
S.display();
}

/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit studclass.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o studclass studclass.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./studclass
Student Id:1
Student Name:Chinmay
Student College:JCJ
Student Class:BCA-II
*/
-------------------------------------------------------------------------------------------------------
Program No. :02
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to accept and display employee information
using class (Define member function outside the class).
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class Employee{
public:
int Id;
string Name;
double Salary;

void print();
};
void Employee::print(){
cout<<"Emp_Id:"<<Id<<endl;
cout<<"Emp_Name:"<<Name<<endl;
cout<<"Emp_Salary:"<<Salary;
}

int main(){
Employee E;
E.Id=01;
E.Name="Abc";
E.Salary=25000;

E.print();
}

/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit empclass.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o empclass empclass.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./empclass
Emp_Id:1
Emp_Name:Abc
Emp_Salary:25000
*/
-------------------------------------------------------------------------------------------------------
Program No. :03 A
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to demonstrate different (public) access
modifiers.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class Student{
public:
int Id;
string Name,College,Class;

void Insert(int i,string n,string c,string r){


Id=i;
Name=n;
College=c;
Class=r;
}
void display(){
cout<<"Student Id:"<<Id<<endl;
cout<<"Student Name:"<<Name<<endl;
cout<<"Student College:"<<College<<endl;
cout<<"Student Class:"<<Class;
}
};
int main(){
Student S;
S.Insert(01,"Chinmay","JCJ","BCA-II");
S.display();
}

/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit public.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o public public.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./public
Student Id:1
Student Name:Chinmay
Student College:JCJ
Student Class:BCA-II
*/
-------------------------------------------------------------------------------------------------------
Program No. :03 B
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to demonstrate different (private) access
modifiers.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class circle
{
private:
int radius;
int area()
{
return 3.14*radius*radius;
}
};
int main()
{
circle obj;
obj.radius=7;
cout<<"radius is:"<<obj.radius<<endl;
cout<<"Area is :"<<obj.area();
return 0;
}
/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit private.c++
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ private private.c++
private.c++: In function ‘int main()’:
private.c++:15:5: error: ‘double circle::radius’ is private within this context
15 | obj.radius=7;
| ^~~~~~
private.c++:6:8: note: declared private here
6 | double radius;
| ^~~~~~
private.c++:16:25: error: ‘double circle::radius’ is private within this context
16 | cout<<"radius is:"<<obj.radius<<endl;
| ^~~~~~
private.c++:6:8: note: declared private here
6 | double radius;
| ^~~~~~
private.c++:17:28: error: ‘double circle::area()’ is private within this context
17 | cout<<"Area is :"<<obj.area();
| ~~~~~~~~^~
private.c++:7:8: note: declared private here
7 | double area()
| ^~~~
*/
-------------------------------------------------------------------------------------------------------
Program No. :03 C
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to demonstrate different (protected) access
modifiers.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class Employee{
protected:
double Salary;

public:

void setVal(double sal){


Salary=sal;
}

double display(){
cout<<"Employee Salary:";
return Salary;
}
};
int main(){
Employee E;
E.setVal(25000);
cout<<E.display();
}

/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit protected.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o protected protected.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./protected
Employee Salary:25000
*/
-------------------------------------------------------------------------------------------------------
Program No. :04
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to perform static data members.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class demo{
public:
static int x;
};

int demo::x = 10;

int main(){
cout<<"The value of static data members:"<<demo::x;
return 0;
}

/*Output:

jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit staticdatamem.cpp


jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o staticdatamem staticdatamem.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./staticdatamem
The value of static data members:10
*/
-------------------------------------------------------------------------------------------------------
Program No. :05
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to demonstrate default arguments to the
function.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
int sum(int x,int y,int z=0,int w=0){
return (x+y+z+w);
}
int main(){
cout<<sum(10,15)<<endl;
cout<<sum(10,15,20)<<endl;
cout<<sum(10,15,20,25);

return 0;
}

/*Output:
jcj-bca-pc16@jcjbcapc16:~/batman$ gedit default.cpp
jcj-bca-pc16@jcjbcapc16:~/batman$ g++ -o default default.cpp
jcj-bca-pc16@jcjbcapc16:~/batman$ ./default
25
45
70
*/
-------------------------------------------------------------------------------------------------------
Program No. :06
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to perform inline function.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
inline int sum(int a, int b){
return (a+b);
}
int main(){
cout<<"The sum of a & b:"<<sum(10,11);
return 0;
}

/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit inline.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o inline inline.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./inline
The sum of a & b:21
*/
-------------------------------------------------------------------------------------------------------
Program No. :07 A
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to demonstrate friend function and friend
class.
-------------------------------------------------------------------------------------------------------
Program Code:

Friend Class:

#include<iostream>
using namespace std;
class Box
{
private :
double width;

public :
friend void printwidth(Box b);
void setwidth(double w);
};

void Box :: setwidth(double w)


{
width = w;
}

void printwidth(Box b)
{

cout<<"The width of box is : "<<b.width<<endl;


}

int main()
{
Box b;
b.setwidth(100);
printwidth(b);
return 0;
}
/*Output:
jcj-bca-pc13@jcjbcapc13:~/bca2-54/cpp$ gedit friend.cpp
jcj-bca-pc13@jcjbcapc13:~/bca2-54/cpp$ g++ -o friend friend.cpp
jcj-bca-pc13@jcjbcapc13:~/bca2-54/cpp$ ./friend
The width of box is : 100
*/
-------------------------------------------------------------------------------------------------------
Program No. :07 B
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to demonstrate friend function and friend
class.
-------------------------------------------------------------------------------------------------------
Friend Class:
Program Code:
#include<iostream>
using namespace std;
class classB;
class classA
{
private:
int numA;
friend class classB;
public:
classA():numA(20)
{
}
};

class classB
{
private:
int numB;
public:
classB() : numB(30)
{
}
int add()
{
classA obj1;
return obj1.numA + numB;
}
};

int main()
{
classB obj2;
obj2.add();
cout<<"Sum:"<<obj2.add()<<endl;
return 0;
}
/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit friendC.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o friendC friendC.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./friendC
Sum:50
*/
-------------------------------------------------------------------------------------------------------
Program No. :08 A
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to demonstrate default constructor.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class CMP{
public:
string name;
string position;
double salary;

CMP(){
cout<<"He work for company"<<endl;
}
};

int main()
{
CMP C;
C.name="Raj";
C.position="worker";
C.salary=25000;

cout<<"Person Name:"<<C.name<<endl;
cout<<"Person Position:"<<C.position<<endl;
cout<<"Person Salary:"<<C.salary<<endl;

return 0;
}

/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit constructor.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o constructor constructor.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./constructor
He work for company
Person Name:Raj
Person Position:worker
Person Salary:25000
*/
-------------------------------------------------------------------------------------------------------
Program No. :08 B
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to demonstrate parameterized constructor.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class CMP{
public:
string name;
string position;
double salary;

CMP(string n,string p,double sal){


name=n;
position=p;
salary=sal;
}
};

int main()
{
CMP C("Raj","worker",25000);

cout<<"Person Name:"<<C.name<<endl;
cout<<"Person Position:"<<C.position<<endl;
cout<<"Person Salary:"<<C.salary<<endl;

return 0;
}
/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit ParaCons.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o ParaCons ParaCons.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./ParaCons
Person Name: Raj
Person Position: worker
Person Salary:25000
*/
-------------------------------------------------------------------------------------------------------
Program No. :08 C
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to demonstrate copy constructor.
-------------------------------------------------------------------------------------------------------
Program Code:
#include<iostream>
using namespace std;
class CMP{
public:
string name;
string position;
double salary;

CMP(string n,string p,double sal){


name=n;
position=p;
salary=sal;
}

CMP(CMP &c){
name=c.name;
position=c.position;
salary=c.salary;
}

void display(){
cout<<"Person Name:"<<name<<endl;
cout<<"Person Position:"<<position<<endl;
cout<<"Person Salary:"<<salary<<endl;
}
};

int main()
{
CMP C("Raj","worker",25000);
CMP C1(C);
C.display();
C1.display();
return 0;
}
/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit copy.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o copy copy.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./copy
Person Name:Raj
Person Position:worker
Person Salary:25000
Person Name:Raj
Person Position:worker
Person Salary:25000
*/
-------------------------------------------------------------------------------------------------------
Program No. :09
Name : Chinmay Chandrakant Terdale.
Class : BCA-II Roll No: 54
Program Name: Write a c++ program to demonstrate desrtuctor.
-------------------------------------------------------------------------------------------------------
Program Code:

#include<iostream>
using namespace std;
class text
{
public:
text()
{
cout<<"Constructor is exectued"<<endl;
}

~text()
{
cout<<"Destructor is exectued"<<endl;
}
};

int main()
{
text t;
return 0;
}

/*Output:
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ gedit destruct.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ g++ -o destruct destruct.cpp
jcj-bca-pc24@jcjbcapc24:~/bca2-54/cpp$ ./destruct
Constructor is exectued
Destructor is exectued
*/

You might also like