Вы находитесь на странице: 1из 7

Write a program to calculate absolute value of a number

#include<stdio.h> int main() { int absolute(int n); int n; printf("Enter a number to find its absolute value"); scanf("%d",&n); printf("The absolute value of %d is %d ",n,absolute(n)); return 0; } int absolute(int n) { if(n<0) return -n; else return n; }

Output :

Enter a number to find its absolute value -5 The absolute value of -5 is 5

Write a program to negate a number using call by reference


#include<stdio.h> int main() { int negate(int *n); int n; printf("Enter a number to negate its value \n"); scanf("%d",&n); printf("The negated value is %d",negate(&n)); return 0; } int negate(int *n) { *n=*n * -1; return *n; }

Output :

Enter a number to negate its value 5 The negated value is 5

Write a Program to observe scope of different variables


#include<iostream> using namespace std; int m=10; int main() { int m=20; // local to main { int k=m; int m=30; cout<<"inner block"<<endl; cout<<"m = "<<m<<endl; cout<<"k = "<<k<<endl; cout<<"::m = "<<::m<<endl; } cout<<"outer block "<<endl; cout<<"m = "<<m<<endl; cout<<"::m = "<<::m<<endl; return 0; } // global scope

Output :
inner block m = 30 k = 20 ::m = 10 outer block m = 20 ::m = 10

Write a function of time using friend function


#include<iostream.h> class time { int hh,mm,ss; public: void input() { cout<<"Enter time cin>>hh>>mm>>ss; }

:";

friend time addtime(time temp1, time temp2) { time temp3; temp3.hh=temp1.hh+temp2.hh; temp3.mm=temp1.mm+temp2.mm; temp3.ss=temp1.ss+temp2.ss; if(temp3.ss>59) { temp3.ss=temp3.ss-60; temp3.mm++; if(temp3.mm>59) { temp3.mm=temp3.mm-60; temp3.hh++; }

} if(temp3.mm>59) { temp3.mm=temp3.mm-60; temp3.hh++; } return temp3; } void output() { cout<<hh<<":"<<mm<<":"<<ss<<endl; } };

int main() { time t1,t2,t3; t1.input(); t2.input(); t3=addtime(t1,t2); cout<<"Added time is :"; t3.output(); return 0; }

Output :
Enter time : 17 02 50 Enter time : 3 07 50 Added time is: 20:10:40

Write A Program to create a class as a friend of preexisting class


#include <iostream> using namespace std; class myclass { int val; friend class changer; //friend class public: myclass(): val(0){} void input() { cout<<"\nEnter value: "; cin>>val; } void output() { cout<<"\nValue : "<<val; } }; class changer { int falseval; public: void change(myclass &m1,int falseval) { m1.val=falseval; } }; int main() { myclass m; changer c; m.input(); m.output(); cout<<"\nModifying value using friend class.\n"; c.change(m, 5); m.output(); return 0; }

Output:
Enter value: 10 Value : 10 Modifying value using friend class. Value : 5

Write A Program to demonstrate Copy Constructor


#include <iostream> using namespace std; class num { int x,y; public: num() { x=0; y=0; } num(num &N) { x=N.x; y=N.y; } void input() { cout<<"\nEnter x: "; cin>>x; cout<<"\nEnter y: "; cin>>y; } void output() { cout<<"\nx : "<<x; cout<<"\ny : "<<y; } }; int main() { num N1; N1.input(); cout<<"\nContents of N1: "; N1.output(); num N2(N1); cout<<"\nContents of N2: "; N2.output(); return 0; }

Output :
Enter x: Enter y: Contents Contents 4 5 of N1: x : 4 y : 5 of N2: x : 4 y : 5

Вам также может понравиться