
i was going through a article about friend function and then in i came across this code:
#include <iostream>
using namespace std;
class Distance
{
private:
int meter;
public:
Distance(): meter(0) { }
friend int addFive(Distance);
};
int addFive(Distance d)
{
d.meter += 5;
return d.meter;
}
int main()
{
Distance D;
cout<<"Distance: "<< addFive(D);
return 0;
}
in the above code what is
Quote: Distance(): meter(0) { }
and how is a object of class distance is able to access a private member ?Quote: int addFive(Distance d)
{
//accessing private data from nonmember function
d.meter += 5;
return d.meter;
}
Thank you.






You should get hold of a good C++ book. Probably the best would be the one written by Bjarne Stroustrup[^]





Welcome to C++ .
I mean:
 constructors are the very basics of an OOP language (you must be sure to grasp them before even attempting to code).
 accessing an otherwise unaccessible member is the very purpose of the
friend declaration.





Problem statement: You are given Q queries. Each query consists of a single number N. You can perform any of the 2 operations on in each move:
1: If we take 2 integers a and b where N=aXb(a!=1,b!=1) then we can change N=max(a,b).
2: Decrease the value of N by 1. Determine the minimum number of moves required to reduce the value of N to 0
Input Format
The first line contains the integer Q.
The next Q lines each contain an integer,N .
Output Format
Output Q lines. Each line containing the minimum number of moves required to reduce the value of N to 0.
Sample Input
2
3
4
Sample Output
3
3
Explanation
For test case 1, We only have one option that gives the minimum number of moves. Follow 3>2 > 1>0 .
Hence, 3 moves.
For the case 2, we can either go 4>3 >2 >1 >0 or4 > 2> 1>0 . The 2nd option is more optimal. Hence, 3 moves.
My algo:
if a number is N then
I do looping until sqrt(N) to find if it is a prime or not.
if it is a prime number then N=N1
if it not a prime number,one of the largest factor(say a) is <=sqrt(N) then other will be b=N/a now b>a then put N=b;
increment count(pass by value).
then next iteration for N ,until it is greater than 1.
return count.
algorithms works fine with small value but predicts less optimal solution for large values.why?





See here.
"One man's wage rise is another man's price increase."  Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it."  Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him."  James D. Miles





i want to make a program that can convert from class a type to class b & vice versa..
i already made a program that does one type of conversion..
#include <iostream>
#include <cmath>
using namespace std;
class Polar;
class Rectangle{
float length, bredth;
public:
Rectangle(){}
Rectangle(float ,float);
int getLength(){return(length);}
int getBredth(){return(bredth);}
void putLength(float x){length = x;}
void putBredth(float y){bredth = y;}
void putData(){cout<<"Length : "<<length<<"\tBreadth : "<<bredth<<endl;}
~Rectangle(){}
};
Rectangle::Rectangle(float x, float y){
length = x;
bredth = y;
}
class Polar{
float angle, rad;
public:
Polar(){}
Polar(float , float);
operator Rectangle(){
Rectangle temp;
float l = rad*cos(angle), b =rad*sin(angle) ;
temp.putLength(l);
temp.putBredth(b);
return(temp);
}
int getAngle(){return(angle);}
int getRadius(){return(rad);}
void putData(){cout<<"Angle : "<<angle<<"\tRadius : "<<rad<<endl;}
~Polar(){}
};
Polar::Polar(float x, float y){
angle = x;
rad = y;
}
int main(){
Rectangle r1(40, 20.23);
Polar p1(120, 25);
r1 = p1;
r1.putData();
return 0;
}
as in the above code i have converted class Polar to class Rectangle, but i also want to have a conversion of class Rectangle to class Polar.
Thank you





Tarun Jha wrote: as in the above code i have converted class Polar to class Rectangle,
Did you test it?
Does it work?
I ask because I see neither the assignment operator
Rectangle& Rectangle::operator =(const Polar& polar) nor the ctor to create Rectangle instance from the Polar one...





but i am not using any variables with reference to heap, so i thought it shouldn't be needed for ex: new, *variable etc.





#include <iostream>
#include <cmath>
using namespace std;
class Polar;
class Rectangle
{
float length, bredth;
public:
Rectangle(){}
Rectangle(float ,float);
operator Polar();
int getLength(){return(length);}
int getBredth(){return(bredth);}
void putLength(float x){length = x;}
void putBredth(float y){bredth = y;}
void putData(){cout<<"Length : "<<length<<"\tBreadth : "<<bredth<<endl;}
~Rectangle(){}
};
class Polar{
float angle, rad;
public:
Polar(){}
Polar(float , float);
operator Rectangle();
int getAngle(){return(angle);}
int getRadius(){return(rad);}
void putAngle(float a){angle=a;}
void putRadius(float r){rad = r;}
void putData(){cout<<"Angle : "<<angle<<"\tRadius : "<<rad<<endl;}
~Polar(){}
};
Rectangle::Rectangle(float x, float y)
{
length = x;
bredth = y;
}
Rectangle::operator Polar()
{
Polar temp;
float a = atan2(bredth, length), r = sqrt(length*length + bredth*bredth);
temp.putAngle(a);
temp.putRadius(r);
return(temp);
}
Polar::Polar(float x, float y)
{
angle = x;
rad = y;
}
Polar::operator Rectangle()
{
Rectangle temp;
float l = rad*cos(angle), b =rad*sin(angle);
temp.putLength(l);
temp.putBredth(b);
return(temp);
}
int main()
{
float radians = 120 * M_PI / 180;
Polar p1(radians, 25);
p1.putData();
Rectangle r1 = p1;
r1.putData();
Polar p2 = r1;
p2.putData();
return 0;
}





thank you






here ih the code below i have tried to add two matrix objects and assign it to the third object but it doesn't give expected result and the rather some garbage value.
include <iostream>
using namespace std;
class matrix{
int **p;
int d1, d2;
public:
matrix(int x=20, int y=20);
void get_element();
void put_element();
int checkAdd(matrix &);
matrix operator + (matrix &);
void operator = (matrix );
~matrix(){
for (int i = 0; i < d1; i++)
delete p[i];
delete p;
}
};
matrix temp;
void matrix::operator = (matrix obj){
this>d1 = obj.d1;
this>d2 = obj.d2;
for(int i=0; i<d1; i++)
for(int j=0; j<d2; j++)
this>p[i][j] = obj.p[i][j];
}
matrix matrix::operator+(matrix &obj){
if(checkAdd(obj)==0)
{
for(int i=0; i<obj.d1; i++)
for(int j=0; j<obj.d2; j++)
temp.p[i][j] = this>p[i][j] + obj.p[i][j];
return(temp) ;
}
cout<<"coloumn and rows of both matrix are not equal !"<<endl;
return (*this);
}
matrix ::matrix(int x, int y)
{
d1 = x;
d2 = y;
p = new int *[d1];
for(int i=0; i<d1; i++)
p[i] = new int [d2];
}
int matrix::checkAdd(matrix &obj){
if((this>d1 == obj.d1) && (this>d2 == obj.d2))
return 0;
else return 1;
}
void matrix::get_element(){
for(int i=0; i<d1; i++)
for(int j=0; j<d2; j++){
cout<<"m["<<i<<"]["<<j<<"] = ";
cin>>p[i][j];
}
}
void matrix::put_element(){
cout<<"\n\n";
for(int i=0; i<d1; i++){
for(int j=0; j<d2; j++){
cout<<p[i][j]<<"\t";
}
cout<<endl;
}
}
int main(){
matrix A(3, 2), B(3, 2), C;
cout<<"Enter matrix elements row by row \n\n";
A.get_element();
A.put_element();
B.get_element();
B.put_element();
C = A + B;
cout<<"\n\n";
C.put_element();
return 0;
}
thank you





What result did you expect and what did you get?





Quote: So basically i want to add 2 matrix object and assign the result to the third noninitiated matrix object, without changing the values of first 2 original matrix.
this is the result i am getting.
Quote: Enter matrix elements row by row
m[0][0] = 1
m[0][1] = 2
m[1][0] = 3
m[1][1] = 4
m[2][0] = 5
m[2][1] = 6
1 2
3 4
5 6
m[0][0] = 7
m[0][1] = 8
m[1][0] = 9
m[1][1] = 10
m[2][0] = 11
m[2][1] = 12
7 8
9 10
11 12
8 10 47120720 0 0 0 0 0 0 0 50331651 41272 47127056 0 47120720 0 0 2396 136 0
12 14 47120720 0 1550742898 1148219457 1549890657 1633906508 1700027500
1426092141 1146242387 1229016399 1162100046 1330924371 1429482832 844253517 1398079566 1329877573 1313423693 1095717471
16 18 47120720 0 1162690894 1918981181 1426091637 1347568979 1229344594
1128088908 1934974010 1551069797 1970430292 1398145134 1127232561 1414417743 1397509967 1547322173 1735357008 544039282
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
47120720 0 47127424 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
i want a result:
8 10
12 14
16 18





You already posted this exact question in the ATL/STL forum, and I gave you a suggestion. If you google for "operator overload C++" you will find plenty of samples.





i was told that these type of questions are to be posted here.
and i did the program..
#include <iostream>
using namespace std;
class matrix{
int **p;
int d1, d2;
int id=0;
public:
matrix(int x=20, int y=20);
int getD_1(){return (d1);}
int getD_2(){return (d2);}
void get_element();
void put_element();
friend int checkAdd(matrix &, matrix &);
friend int checkMul(matrix &, matrix &);
friend matrix operator+(matrix &, matrix &);
friend matrix operator*(matrix &, matrix &);
void operator = (matrix );
~matrix(){
for (int i = 0; i < d1; i++)
delete p[i];
delete p;
}
};
void matrix::operator = (matrix obj){
this>d1 = obj.d1;
this>d2 = obj.d2;
this>id = obj.id;
for(int i=0; i<d1; i++)
for(int j=0; j<d2; j++)
this>p[i][j] = obj.p[i][j];
}
matrix operator+(matrix &a, matrix &b){
matrix temp;
temp.d1 = a.d1;
temp.d2 = a.d2;
temp.id = 1;
if(checkAdd(a, b) == 0){
for(int i=0; i<temp.d1; i++)
for(int j=0; j<temp.d2; j++)
temp.p[i][j] = a.p[i][j] + b.p[i][j] ;
return(temp);
}
else {
cout<<"Matrix addition not possible as rows and coloumn's of given 2 matrices are not equal"<<endl;
return(a);
}
}
matrix operator*(matrix &a, matrix &b){
matrix temp;
temp.d1 = a.d1;
temp.d2 = b.d2;
temp.id = 2;
if(checkMul(a, b) == 0){
int sum=0;
for (int c = 0; c < a.d1; c++) {
for (int d = 0; d < b.d2; d++) {
for (int k = 0; k <b.d1; k++) {
sum = sum + a.p[c][k]*b.p[k][d];
}
temp.p[c][d] = sum;
sum = 0;
}
}
return (temp);
}
else {
cout<<"Matrix Multiplication is not possible as coloumn of 1st Matrix != Row of 2nd Matrix "<<endl;
return(a);
}
}
matrix ::matrix(int x, int y){
d1 = x;
d2 = y;
p = new int *[d1];
for(int i=0; i<d1; i++)
p[i] = new int [d2];
}
int checkAdd(matrix &a, matrix &b){
if(a.getD_1() == b.getD_1() && a.getD_2() == b.getD_2())
return 0;
else return 1;
}
int checkMul(matrix &a, matrix &b){
if(a.getD_2() == b.getD_1())
return 0;
else return 1;
}
void matrix::get_element(){
for(int i=0; i<d1; i++)
for(int j=0; j<d2; j++){
cout<<"m["<<(i+1)<<"]["<<(j+1)<<"] = ";
cin>>p[i][j];
}
}
void matrix::put_element(){
if(id == 1) cout<<"\nThe result of addition of 2 matrices is :"<<endl;
else if(id==2) cout<<"\nThe result of product of 2 matrices is:"<<endl;
for(int i=0; i<d1; i++){
for(int j=0; j<d2; j++){
cout<<p[i][j]<<"\t";
}
cout<<endl;
}
cout<<"\n";
}
int main(){
matrix A(3, 2), B(2, 4), C;
cout<<"Enter matrix elements row by row \n\n";
A.get_element();
A.put_element();
B.get_element();
B.put_element();
C = A * B;
C.showId();
C.put_element();
C = A + B;
C.showId();
C.put_element();
return 0;
}
and thank you for the advice





Forget about matrix calculations. You need to go and study operator overloading and get that working on a basic class first. Once you understand how to do it correctly so you get no errors, then you can move on to the matrix functions.





is there anything wrong with the program i have posted before ?





I don't know; you have to do your own testing. But I find one of the easiest way to debug issues is to create a small test program that just focused on the area that appears not to work. So a small program with a very basic class, that just tests the operator+ overload, would be much easier to understand.





ok...
thanks for the advice





Broken 5V supply to LCD!
I should have know better since the LCD did not react to powerup  reset and show 5/7 characters  but the power LED was on on the i2c module itself !
Learned few Linux tools on how to troubleshoot i2c, so no big loss.
Now I can proceed with implementing ioctl.
I would like to discuss implementation of Linux ioctl function.
There are few reasons I am posting my question here and not on Linux forum. However, if you feel it is inappropriate you have two options  ignore my request ( preferred to sermon ) or flag it to be removed.
I am basically aware WHAT ioctl does, and have been thru few documents ( Linux Device drivers, CodeProject driver tutorials etc.)
I am trying to implement ioctl "driver" to output (Raspberry Pi) to I2C LCD.
The problems I cannot get straight is how to configure I2C speed (using C++, not Linux commands ) and implement ACK read from slave I2C device.
I understand I2C protocol, but all of the sample codes I have read so far are "limited" to open ioctl file and "write " to I2C slave address.
I like to see or make my own code to implement I2C "start /stop " and read ACK/ NACK from slave.
Any to the point help will as always be appreciated.
Thanks
 modified 7Apr18 15:35pm.





You have two options:
 Use the existing I2C support on the Raspberry Pi with the I2C GPIO pins
 Implement your own I2C support on general use GPIO pins
The I2C speed is defined by the master by generating the clock. A device might use "clock stretching" to slow down (Clock, Stretching, Arbitration  I2C Bus[^]).
So implementations are not time critical. They can use a predefined (max.) clock and have not to care about being slowed down by other tasks on the system.
I have implemented I2C in assembler on micro controllers. Implementing it in C is not a problem but be aware that it might be a frustrating tasks because it can't be simply debugged. I highly recommend to use a logic analyser or at least a storage oscilloscope to check the signals on the bus.
Quote: all of the sample codes I have read so far are "limited" to open ioctl file and "write " to I2C slave address. With device drivers use i2c_transfer() . Have a look at the source of any Linux driver for a I2C device like rtcpcf8563.c
[^].
When using ioctl() on the I2C device just use read() :
int fd = open("/dev/i2c1", O_RDWR);
ioctl(fd, I2C_SLAVE, addr);
read(fd, buffer, length); Or use ioctl(I2C_RDWR) or ioctl(I2C_SMBUS) for reading which both does not require the I2C_SLAVE[_FORCE] . In any case check first if the used ioctl() call is supported.





The speed is configured outside the program or on the library you are using on the Pi
Edit /boot/config.txt if it doesn't exist create it
add the line or edit the line with your baudrate
dtoverlay=i2cspeed,i2c1_baudrate=400000
i2c1 may also be disabled by default I can't remember if so you need
dtparam=i2c_vc=on
In the userland repository there is also a direct header and library
bcm2835: C library for Broadcom BCM 2835 as used in Raspberry Pi[^]
and a forum
bcm2835 : Google Groups[^]
Standard use
#include <bcm2835.h>
int main(int argc, char **argv)
{
char buf[1];
if (!bcm2835_init())return 1;
bcm2835_i2c_begin();
bcm2835_i2c_setSlaveAddress(0x20);
bcm2835_i2c_set_baudrate(10000);
while(1)
{
buf[0] = 0xEF;
bcm2835_i2c_write(buf,1);
bcm2835_delay(500);
buf[0] = 0xFF;
bcm2835_i2c_write(buf,1);
bcm2835_delay(500);
}
bcm2835_i2c_end();
bcm2835_close();
return 0;
}
In vino veritas
modified 26Mar18 23:06pm.





Thanks for replies.
I am currently "on the road" and will reply when I get home.
In short
I did try "just GPIO" , it worked to the point, but it got too convoluted.
Hence I am after using ioctl.
I'll take a look at the bcm2835.h, hopefully I can use it for bcm2837 chip used in RPi 3B.
I think my main concern is still to be able to "see" the code following / running the I2C spec  mainly changing hardware pins directions and reading START /STOP and ACK/NACK back. Of course I am verifying the actual LCD and sometime even using scope.
Thanks for all your comments, appreciate that.
 modified 5Apr18 12:42pm.



